«Back to Home

Core Java

Topics

How To Perform Single Task By Multiple Threads In Java

In Java, if we have to perform a single task by many threads, we need to use only one run () method.
 
Performing single task by multiple threads

Let’s see an example, given below.
 
Code
  1. public class MultipleThreads extends Thread {  
  2.     public void run() {  
  3.         System.out.println("Task one is start now..");  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         MultipleThreads mt1 = new MultipleThreads();  
  7.         MultipleThreads mt2 = new MultipleThreads();  
  8.         MultipleThreads mt3 = new MultipleThreads();  
  9.         mt1.start();  
  10.         mt2.start();  
  11.         mt3.start();  
  12.     }  
  13. }  
39
 
Output

40

In the example, mentioned above example, we created multipleThreads class extends thread class and performed a single task through the multiple threads.
 
Let’s see another example with Runnable interface, mentioned blow.
 
Code
  1. public class MultipleThreads implements Runnable {  
  2.     public void run() {  
  3.         System.out.println("Task one is start now….");  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         Thread t1 = new Thread(new MultipleThreads()); //passing annonymous object  
  7.         Thread t2 = new Thread(new MultipleThreads());  
  8.         Thread t3 = new Thread(new MultipleThreads());  
  9.   
  10.         t1.start();  
  11.         t2.start();  
  12.         t3.start();  
  13.     }  
  14. }  
42

Output

44

In the example, mentioned above, we create multipleThreads class, implement the Runnale interface instead of extending threads and perform a single task through the multiple threads.
 
Each thread runs within a separate callstack in multithreading.

How to perform multiple tasks by multiple threads in Java

In Java, if we have to perform multiple tasks by many threads, we need to use multiple run () method.
 
Performing multiple task by multiple threads

Let’s see an example, given below.
 
Code
  1. class Multi1 extends Thread {  
  2.     public void run() {  
  3.         System.out.println("Task one is start now...");  
  4.     }  
  5. }  
  6. class Multi2 extends Thread {  
  7.     public void run() {  
  8.         System.out.println("Task two is start now...");  
  9.     }  
  10. }  
  11. public class MultipleThreads {  
  12.     public static void main(String args[]) {  
  13.         Multi1 t1 = new Multi1();  
  14.         Multi2 t2 = new Multi2();  
  15.   
  16.         t1.start();  
  17.         t2.start();  
  18.     }  
  19. }  
45

Output

46

In the example, mentioned above, we created three classes and two classes extend the thread class to perform the multiple tasks.
 
Let’s see another example- annonymous class with Runnable interface, given below.
 
Code
  1. public class MultipleThreads {  
  2.     public static void main(String args[]) {  
  3.         Runnable r1 = new Runnable() {  
  4.             public void run() {  
  5.                 System.out.println("Task one is start now...");  
  6.             }  
  7.         };  
  8.         Runnable r2 = new Runnable() {  
  9.             public void run() {  
  10.                 System.out.println("Task two is start now...");  
  11.             }  
  12.         };  
  13.         Thread t1 = new Thread(r1);  
  14.         Thread t2 = new Thread(r2);  
  15.         t1.start();  
  16.         t2.start();  
  17.     }  
  18. }  
47
 
Output

48

In the example, mentioned above, we created MultipleThreads class, use Runnable interface to perform the multiple tasks.
 
Let’s see another example- anonymous class that extends thread class, given below.
 
Code
  1. public class MultipleThreads {  
  2.     public static void main(String args[]) {  
  3.         Thread t1 = new Thread() {  
  4.             public void run() {  
  5.                 System.out.println("Task one is start now...");  
  6.             }  
  7.         };  
  8.         Thread t2 = new Thread() {  
  9.             public void run() {  
  10.                 System.out.println("Task two is start now...");  
  11.             }  
  12.         };  
  13.         t1.start();  
  14.         t2.start();  
  15.     }  
  16. }  
49

Output

50

In the example, mentioned above, we created MultipleThreads class, extended the thread class to perform the multiple tasks.
 
Summary

Thus, we learnt, if we have to perform a single task by many threads, we need to use only one run () method and also learnt, if we have to perform multiple tasks by many threads, we need to use multiple run () method.