• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Parent Thread should wait till all child thread are dead

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Point well taken. Tx for the comment
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of your inputs has helped me to solve my problem.
Thanks to all.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All !!

It solved my problem too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic