Kevin Kevin - 1 year ago 76
Java Question

Threads in Java

I was today asked in an interview over the Thread concepts in Java? The Questions were...

  1. What is a thread?

  2. Why do we go for threading?

  3. A real time example over the threads.

  4. Can we create threads in Spring framework service class.

  5. Can flex call a thread?

I did not answer any questions apart from definition of Thread, that too I just learnt from internet.

Can anyone explain me clearly over this.


What is a difference between a thread and a normal java class.
why do we need threading... can i execute business logic in threads.
Can i call a different class methods in Threads.

Answer Source

To create threads, create a new class that extends the Thread class, and instantiate that class. The extending class must override the run() method and call start() method to begin execution of the thread.

Inside run(), you will define the code that constitutes a new thread. It is important to understand that run() can call other methods, use other classes and declare variables just like the main thread. The only difference is the run() establishes the entry point for another, concurrent thread of execution within your program. This will end when run() returns.

public class MyThread extends Thread {

  String word;

  public MyThread(String rm){
    word = rm;

  public void run(){

    try {


    } catch(InterruptedException e) {

      System.out.println("sleep interrupted");      

  public static void main(String[] args) {

    Thread t1=new MyThread("First Thread");
    Thread t2=new MyThread("Second Thread");

Output screen:

First Thread
Second Thread
First Thread
Second Thread
First Thread

and see this :

and for Spring you can use thread pool :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download