| Author |
Parent Thread should wait till all child thread are dead
|
Purujit Saha
Ranch Hand
Joined: Nov 01, 2005
Posts: 86
|
|
I have a Parent Class which spawns 10 child threads, class Parent{ public static void main(String[] args) { int threadWorkerNo = 10; processStartTime = System.currentTimeMillis(); spawnThreads(threadWorkerNo); System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); } private static void spawnThreads(int number){ for(int i=1;i<=number; i++){ String name = Integer.toString(i); MigrationWorker th = new MigrationWorker(name); th.startThread(); } } } Assume the MigrationWorker is a Thread Class which does some work. now i want the Parent class main method to wait till all the 10 migration workers thread are died. That means when all the 10 child threads are dead,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& will be printed. Thanks in advance.... [ April 09, 2007: Message edited by: Purujit Saha ] [ April 09, 2007: Message edited by: Purujit Saha ]
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
You can use the following code to do the needful. P.S: MyThread is just a dummy Implementation. It will be replaced by the actual thread that does the work you want to do. [ April 09, 2007: Message edited by: Nitesh Kant ]
|
apigee, a better way to API!
|
 |
Chris Beckey
Ranch Hand
Joined: Jun 09, 2006
Posts: 116
|
|
If you have JDK 1.5 or later then this should also work. There are also ways to do this with queued executor services (using Futures) if you want real thread pool management (please note the comments concerning queued executor services).
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Your spawnThreads() method doesn't save any of the thread objects for the threads that it created. If it did (such as saving the objects in a collection which is returned from the method), you can later have your main thread call join() on all the thread objects, to wait for them all to complete. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
Originally posted by Henry Wong: Your spawnThreads() method doesn't save any of the thread objects for the threads that it created. If it did (such as saving the objects in a collection which is returned from the method), you can later have your main thread call join() on all the thread objects, to wait for them all to complete. Henry
Henry, firstly i am delighted to communicate with the author of the book i learned Threads from !!! Keeping aside the requirement in this problem, is it a good idea to use a wait-notify mechanism against the join paradigm? One scenario can be when threads are not computation intensive and finish their work pretty soon. So, will it be more effecient to use a wait-notify mechanism over "joining". The reason being that the parent will unnecessarily try to join even if the worker thread has already finished its job? Worth mentioning, its just a hypothetical situation for academic reasons!
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
join() is, in fact, implemented in Java code using wait() and notify(). The first example is just a terribly complex way of implementing Henry's suggestion by hand. If I saw code like that in a review, I'd make the author change it to use join() in a loop.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
|
Point well taken. Tx for the comment
|
 |
Purujit Saha
Ranch Hand
Joined: Nov 01, 2005
Posts: 86
|
|
All of your inputs has helped me to solve my problem. Thanks to all.
|
 |
Shiaber Shaam
Ranch Hand
Joined: Jun 16, 2006
Posts: 252
|
|
Thanks All !!
It solved my problem too.
|
$Hi/\bEr
|
 |
 |
|
|
subject: Parent Thread should wait till all child thread are dead
|
|
|