Perform Single And Multiple Task Using Multiple-Thread In Java

This article explains how to perform a single and multiple tasks using multiple threads.

Multithreading in Java

 
This article explains how to perform a single and multiple tasks using multiple threads.
 

Performing single task

 
If we need to perform a single task using multiple threads then we need to use only one/single run() method.
 
Example
 
In this example, we simply extend the thread and perform a single task using multiple threads.
  1. class MultithreadEx1 extends Thread  
  2. {  
  3.     public void run()  
  4.     {  
  5.         System.out.println("Start task one");  
  6.     }  
  7.     public static void main(String args[])  
  8.     {  
  9.         MultithreadEx1 th1 = new MultithreadEx1();  
  10.         MultithreadEx1 th2 = new MultithreadEx1();  
  11.         MultithreadEx1 th3 = new MultithreadEx1();  
  12.         th1.start();  
  13.         th2.start();  
  14.         th3.start();  
  15.     }  
  16. }  
Output
 
Fig-1.jpg
 
Example 2
 
In this example we use the Runnable interface instead of extending threads.
  1. class MultithreadEx2 implements Runnable  
  2. {  
  3.     public void run()  
  4.     {  
  5.         System.out.println("Start task one");  
  6.     }  
  7.     public static void main(String args[])  
  8.     {  
  9.         Thread th1 = new Thread(new MultithreadEx2());  
  10.         Thread th2 = new Thread(new MultithreadEx2());  
  11.         Thread th3 = new Thread(new MultithreadEx2());  
  12.         th1.start();  
  13.         th2.start();  
  14.         th3.start();  
  15.     }  
  16. }  
Output
 
Fig-2.jpg
 

Performing multiple tasks

 
To perform multiple tasks by multiple threads, we need to use multiple run() methods.
 
Example 1
 
In this example; we need to extend the thread to perform multiple tasks.
  1. class MultithreadEx3 extends Thread  
  2. {  
  3.     public void run()  
  4.     {  
  5.         System.out.println("Start task one");  
  6.     }  
  7. }  
  8. class MultithreadEx4 extends Thread  
  9. {  
  10.     public void run()  
  11.     {  
  12.         System.out.println("Start task two");  
  13.     }  
  14. }  
  15. class Run  
  16. {  
  17.     public static void main(String args[])  
  18.     {  
  19.         MultithreadEx3 th1 = new MultithreadEx3();  
  20.         MultithreadEx4 th2 = new MultithreadEx4();  
  21.         th1.start();  
  22.         th2.start();  
  23.     }  
  24. }  
Output
 
fig-3.jpg
 
Example 2
 
In this example, we use an anonymous class that extends the Thread class.
  1. class Check  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.         Thread th1 = new Thread()  
  6.         {  
  7.             public void run()  
  8.             {  
  9.                 System.out.println("Start task one");  
  10.             }  
  11.         };  
  12.         Thread th2 = new Thread()  
  13.         {  
  14.             public void run()  
  15.             {  
  16.                 System.out.println("Start task two");  
  17.             }  
  18.         };  
  19.         th1.start();  
  20.         th2.start();  
  21.     }  
  22. }  
Output
 
Fig-4.jpg
 
Example 3
 
In this example, we use an anonymous class that implements the Runnable interface.
  1. class Check1  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.         Runnable run1 = new Runnable()  
  6.         {  
  7.             public void run()  
  8.             {  
  9.                 System.out.println("Start task one");  
  10.             }  
  11.         };  
  12.         Runnable run2 = new Runnable()  
  13.         {  
  14.             public void run()  
  15.             {  
  16.                 System.out.println("Start task two");  
  17.             }  
  18.         };  
  19.         Thread th1 = new Thread(run1);  
  20.         Thread th2 = new Thread(run2);  
  21.         th1.start();  
  22.         th2.start();  
  23.     }  
  24. }  
Output
 
fig-5.jpg