All for Joomla All for Webmasters

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.   t1.start();
  10.   t2.start();
  11.   t3.start();
  12.  }
  13. }
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. public static void main(String args[]){
  6. Thread t1 =new Thread(new TestMultitasking2());//passing annonymous object of TestMultitasking2 class
  7. Thread t2 =new Thread(new TestMultitasking2());
  8. t1.start();
  9. t2.start();
  10.  }
  11. }
Output:task one
       task one

Note: Each thread run in a separate callstack.

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. class Simple2 extends Thread{
  7.  public void run(){
  8.    System.out.println(“task two”);
  9.  }
  10. }
  11.  class TestMultitasking3{
  12.  public static void main(String args[]){
  13.   Simple1 t1=new Simple1();
  14.   Simple2 t2=new Simple2();
  15.   t1.start();
  16.   t2.start();
  17.  }
  18. }
Output:task one
       task two

Same example as above by annonymous class that extends Thread class:

Program of performing two tasks by two threads

  1. class TestMultitasking4{
  2.  public static void main(String args[]){
  3.   Thread t1=new Thread(){
  4.     public void run(){
  5.       System.out.println(“task one”);
  6.     }
  7.   };
  8.   Thread t2=new Thread(){
  9.     public void run(){
  10.       System.out.println(“task two”);
  11.     }
  12.   };
  13.   t1.start();
  14.   t2.start();
  15.  }
  16. }
Output:task one
       task two

Same example as above by annonymous class that implements Runnable interface:

Program of performing two tasks by two threads

  1. class TestMultitasking5{
  2.  public static void main(String args[]){
  3.   Runnable r1=new Runnable(){
  4.     public void run(){
  5.       System.out.println(“task one”);
  6.     }
  7.   };
  8.   Runnable r2=new Runnable(){
  9.     public void run(){
  10.       System.out.println(“task two”);
  11.     }
  12.   };
  13.   Thread t1=new Thread(r1);
  14.   Thread t2=new Thread(r2);
  15.   t1.start();
  16.   t2.start();
  17.  }
  18. }
Output:task one
       task two
PinIt
submit to reddit

Leave a Reply

Top