How to perform multiple tasks by multiple threads (multitasking in multithreading)?


If you have to perform multiple tasks by multiple threads,have multiple run() methods.For example: Program of performing two tasks by two threads

  1. class Simple1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("task one");  
  4.  }  
  5. }  
  6.   
  7. class Simple2 extends Thread{  
  8.  public void run(){  
  9.    System.out.println("task two");  
  10.  }  
  11. }  
  12.   
  13.  class TestMultitasking3{  
  14.  public static void main(String args[]){  
  15.   Simple1 t1=new Simple1();  
  16.   Simple2 t2=new Simple2();  
  17.   
  18.   t1.start();  
  19.   t2.start();  
  20.  }  
Output:task one
       task two



How to perform single task by multiple threads?

If you have to perform single task by many threads, have only one run() method.For example: Program of performing single task by multiple threads
  1. class TestMultitasking1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("task one");  
  4.  }  
  5.  public static void main(String args[]){  
  6.   TestMultitasking1 t1=new TestMultitasking1();  
  7.   TestMultitasking1 t2=new TestMultitasking1();  
  8.   TestMultitasking1 t3=new TestMultitasking1();  
  9.   
  10.   t1.start();  
  11.   t2.start();  
  12.   t3.start();  
  13.  }  
  14. }  

Output:task one
       task one
       task one
Program of performing single task by multiple threads
  1. class TestMultitasking2 implements Runnable{  
  2. public void run(){  
  3. System.out.println("task one");  
  4. }  
  5.   
  6. public static void main(String args[]){  
  7. Thread t1 =new Thread(new TestMultitasking2());//passing annonymous object of TestMultitasking2 class  
  8. Thread t2 =new Thread(new TestMultitasking2());  
  9.   
  10. t1.start();  
  11. t2.start();  
  12.   
  13.  }  
  14. }  

Output:task one
       task one

Note: Each thread run in a separate callstack.

MultipleThreadsStack

What if we call run() method directly instead start() method?

  • Each thread starts in a separate call stack.
  • Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.
  1. class TestCallRun1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("running...");  
  4.  }  
  5.  public static void main(String args[]){  
  6.   TestCallRun1 t1=new TestCallRun1();  
  7.   t1.run();//fine, but does not start a separate call stack  
  8.  }  
  9. }  

Output:running...
 
MainThreadStack 
 
Problem if you direct call run() method
  1. class TestCallRun2 extends Thread{  
  2.  public void run(){  
  3.   for(int i=1;i<5;i++){  
  4.     try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}  
  5.     System.out.println(i);  
  6.   }  
  7.  }  
  8.  public static void main(String args[]){  
  9.   TestCallRun2 t1=new TestCallRun2();  
  10.   TestCallRun2 t2=new TestCallRun2();  
  11.    
  12.   t1.run();  
  13.   t2.run();  
  14.  }  
 
Output:1
       2
       3
       4
       5
       1
       2
       3
       4
       5
 
As you can see in the above program that there is no context-switching because here t1 and t2 will be treated as normal object not thread object.
 
 
 

Can we start a thread twice ?



No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

Let's understand it by the example given below:
  1. public class TestThreadTwice1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("running...");  
  4.  }  
  5.  public static void main(String args[]){  
  6.   TestThreadTwice1 t1=new TestThreadTwice1();  
  7.   t1.start();  
  8.   t1.start();  
  9.  }  
  10. }  
  11.  
  12.  
  13. Output:
     running
           Exception in thread "main" java.lang.IllegalThreadStateException