This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
SCJP - 1.4
SCJD - (B&S) Used 1.5 And It Runs On Solaris10
SCWCD - Thanks to HFSJ!!
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.