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



No comments:

Post a Comment