This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does join() Really Work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Does join() Really Work?" Watch "Does join() Really Work?" New topic

Does join() Really Work?

Bob Nedwor
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

Please help if you can. Sleep() and yield () were cake, but I can't get join () to work. I have yet to find an example of the join() method that actually compiles and runs like they say in the book. According to Chapter 9 in my K&B book, we should be able to use the join method to take a Thread that has already been partially run and temporarily send it back to the runnable state until after a 2nd Thread is finished running, at which point then the original Thread will now go back from runnable to running to complete the remainder of the work. Thanks so much to anyone who can show me some source code that will compile and run (1.4.x) to demonstrate this. For example, they say that if I want to hold off on finishing Thread A until thread B is finished, my syntax would be B.join() but where does this go? in the run() method code of the A thread object, I would think, right? I would think that we should be able to do this if both the runnable targets are run methods in objects that implement the Runnable interface, rather than extend the Thread Class, considering that the first main point in the Chapter is the the former is much better. Thanks so much for any hints. --Bob
[ September 10, 2005: Message edited by: Bob Nedwor ]

Bob N
SCJP - 1.4
SCJD - (B&S) Used 1.5 And It Runs On Solaris10
SCWCD - Thanks to HFSJ!!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Your explanation is way more complex than it needs to be. If "t" is a Thread that hasn't completed, then "t.join()" doesn't return until t has completed. All the rest is just unnecessary jargon.

Here's a complete program that should demonstrate this:

With the "t.join()", you'll see "In the new thread" print first, every time. Without it, you'll see "Back in main thread" first, most of the time, anyway.

Note that if you're worried about a race condition here, don't be. The possible race is that the child thread completes before join() is called -- in which case, the program works as I say it will, right? This is a safe pattern for starting one or more threads, and waiting for them to complete before proceeding.

[Jess in Action][AskingGoodQuestions]
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ernest Friedman-Hill:
... If "t" is a Thread that hasn't completed, then "t.join()" doesn't return until t has completed. All the rest is just unnecessary jargon...

Wow, I've never been clear on this method, so thank you for this explanation!

It seems obvious now that I look back at the API ("Waits for this thread to die."), but I really thought it was more complicated.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Bob Nedwor
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

Thanks so much, Ernest! Yes, this makes everything much clearer. I have no idea why they didn't put an example like this in the book.
It is sorta covered in the JavaRanch Style Guide.
subject: Does join() Really Work?
It's not a secret anymore!