• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to block child thread until parent thread terminates  RSS feed

 
Farrukh Najmi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for a simple way to do this:

* Parent thread starts a child thread
* Child thread blocks until parent thread terminates and then does its processing

What do folks advice? TIA for your help.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends. Do you need just a block of the parent thread that you control to finish before the child runs? If so, you are best off using one of the locking mechanisms (synchronized() or java.util.concurrent locks).

If you want the entire parent thread to die then you would pass a reference of the parent thread to the child and you call join on it.

Examples:
 
Pedro Fracarolli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about trying to start the child thread after the parent has done it's job? You could put a call to the start() method as the last statement in the parent's run() code.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!