Monday, October 17, 2011

Multithread java class

Here is a sample class to run multiple thread in a class. All threads are started in a for loop and after all threads are finished thread join function will wait for all thread to finish.

public class MultiThreadRules {

class MultiThreads implements Runnable {
   private String rulePath;
   public MultiThreads(String rulePath){
                this.rulePath = rulePath;



for(int i=0; i<=5, i++){ //number of threads
this.inputValue = inputValue;
Thread t = new Thread(this);
t.start(); //all threads started
threads.add(t);  
}

try {
for(Thread tt:threads){
tt.join(); //all threads are done
}

} catch (InterruptedException e) {
logger.error(e.getMessage());
}




     }
     public void run() {
      //perform action here
     }
  }


}

No comments:

Post a Comment