• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does join() Really Work?

 
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic