Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can parent thread determine when a child thread finishes execution 1

Status
Not open for further replies.

netooi25

Programmer
Jul 30, 2004
25
US
Hi all, I'm relatively new to java...I havent used it all
that often but I love the ease threads and synchronization.. I need to know if I create a chld thread to to a task, can I determine in the parent thread when child thread finishes executing?

Can you give me some short code example of how to do this?
Thanks.
Netooi
 
If i understand the join method correct, it will stop the current thread until the child thread finishes... This would be good but I may have up to four child threads and if I wait like that the other 3 other three child threads may finish before the one I waited on. I need to be able to detect when they finish without waiting if possible because I want to be able to immediately spawn a new thread and try to keep 4 child threads running at the same time.

Thanks if any tips
Netooi
 
Threads inherently give you two main objects :

- Spawn a thread in a detached state to perform some task that does not interrupt the parent's thread execution.

- Spawn a thread and wait for it to complete.

If you want to achieve option 1, but also be notified when that thread has completed, you need some kind of callback capability.

I have achieved this in the past by passing in some kind of hashtable and a thread id to the the thread's constructor, and then updating this hashtable with a "done" value.
The parent process should poll that hashtable looking for "done" values.

You need to make sure that the setDone() and getDone() methods are synchronized to ensure thread safety in your app.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top