Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does join() Really Work?

 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic