I am implementing java Runnable interface for multithreading. I have some n number of threads. Each of the thread has its own life. I would like to wait till life of all the threads get expire. Lets say following is the case
for(int i = 0; i< k;i++){
Thread thread1 = new Thread(new xyz())
Thread thread2 = new Thread(new abc())
Thread thread3 = new Thread(new mno())
thread1.start();
thread2.start();
thread3.start();
}
I am doing following to synchronize it. I don't know if it is correct. Please let me know how would I do it? And is there any way to check, if my threaded program is working correctly?
if(thread2.isAlive())
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(thread1.isAlive())
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(thread3.isAlive())
try {
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}