this way i am generating threads,but after for loop how to check that all threads are alive or not
further process depends on the all threads
if threads are dead then go for further process
for (int i = 0;i<List.size();i++) {
if(List.get(i).trim().length() >0 ){
Thread thread = new Thread("Thread"+i){
public void run(){
threading(List.get(i),ctx,userSesionObj);
}
};
thread.start();
}
}
here threading is my function which perform on different data that i pass
You can use an ExecutorService. I’d strongly recommend this, in fact. You can specify different threading behaviour etc. The executor will return a Future object for each submitted thread, and then you can iterate through each
Futurecollecting the results. So the following:will complete when all the
Futureshave returned data (i.e. when the threads have completed). The Executor API works at a higher level thanjoin()/notify()etc. and is a lot safer to use.There’s a lot of features to make use of here and I would recommend the tutorials.