aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread.join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread.join()" Watch "Thread.join()" New topic
Author

Thread.join()

Manuj Bawa
Greenhorn

Joined: Dec 13, 2004
Posts: 7
Hi,

Can anyone post up a code example of just Thread.join(), Where should ThreadInstance.join() be placed? I tried writing this code:


To my understanding, the problem here is: when the compiler hits try catch block, main() is still the running thread, My question is: How do I make sure that join is called when a is the current running Thread.

Thanks,
Manuj
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
If a ("First") has to be the current thread you have to call b.join() from it, i.e. from MyRunnableA.run().
What are you trying to accomplish?
Manuj Bawa
Greenhorn

Joined: Dec 13, 2004
Posts: 7
I am just trying to write program that demonstrates join(). I wanted <a> to wait up till <b> finishes.

In the run() method of MyRunnableA, Can I say b.join()?

Thanks,
Manuj
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
You can't say b.join() unless b is in scope. Here's a program that illustrates a way you could show what you've suggested:


[ April 15, 2005: Message edited by: Joe Sanowitz ]

SCJA 1.0 (98%), SCJP 1.4 (98%)
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
If you want a to wait up till b is finished then you will have to get reference b to a somehow and get a to call b.join(). Here is a modified version of your code which will do it for you:

I've just changed the constructor and the reference declaration, and tested it to see that a waits for b to finish (and it does...)




Hope this helps...

regards
Saurabh
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
Actually you can also remove

the following from main, as that is joining main to b, main will wait until b is finished! (but we want a to wait until b is finished?)



Saurabh
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"Manuj", welcome to JavaRanch! Please read our JavaRanch Naming Policy and change your displayed name to conform with it. In your case we require a displayed name in the format <first name> <family name> (in that order).
Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread.join()