I am a new bie to the world of threads , still learning, as I was going through the concept of threads and join where other thread wait for earlier thread to be completed and join it from that end, could you please advise me that that I want to start three threads T1,T2,T3 in which t2 will start once T1 is completed .
Asked
Active
Viewed 68 times
0
-
5If you only want to have one thread running at a time, why bother creating three threads at all? – Jon Skeet Feb 18 '13 at 17:03
-
I think there's also T3. – Vlad Feb 18 '13 at 17:04
-
1You can find a simple solution here: http://stackoverflow.com/a/13695190/469220 – Vlad Feb 18 '13 at 17:05
-
@Vlad well you can do T1 and T2 tasks in the same thread thus having 2 threads instead of 3. – UmNyobe Feb 18 '13 at 17:16
-
@UmNyobe, yeah, you're right. I didn't think it through. – Vlad Feb 18 '13 at 18:36
3 Answers
2
Of what I understand you want to wait until Thread 1 is completely done and then start Thread 2, while thread 3 can run anywhere. Simple code that I think fulfills your question:
Thread thread1 = new Thread1();
Thread thread2 = new Thread2();
Thread thread3 = new Thread3();
thread3.start();
thread1.start();
try {
thread1.join();
thread2.start();
} catch (InterruptedException e) {
//if you do not use thread1.interrupt() this will not happen.
}

ddmps
- 4,350
- 1
- 19
- 34
0
You can use Barriers to start some action (maybe another thread) after a number of threads finished.
Check: http://programmingexamples.wikidot.com/java-barrier for more information.
But to wait for only one thread really doesn't make to much sense...

patrickuhlmlann
- 342
- 2
- 4
- 14
0
Do something like this:
Thread T1 = new Thread(new ThreadExm); // where ThreadExm implements Runnable
Thread T2 = new Thread(new ThreadExm);
try {
// Start the thread1 and waits for this thread to die before
// starting the thread2 thread.
T1.start();
T2.join();
// Start thread2 when T1 gets completed
thread2.start();
} catch (InterruptedException ex) {
ex.printStackTrace();
}

exexzian
- 7,782
- 6
- 41
- 52