This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, the join method in the Thread class makes a thread wait for the other thread until the second one dies. While the first one is waiting, are the locks it holds automatically released ? The API doesnt mention that. Second question: If you have multiple nested synchronized blocks and in the deepest one you have a wait(). Does the thread release locks on all the synchronized vars or only on the object in the deepest synchronized expression ? For example:
As far as your question with the join goes, it appears that no locks are released when you invoke join. I wasn't sure about this so I wrote this little application:
Notice that I'm using the version of join that allows for a maximum wait time. I used the argument 10,000, which should cause it to wait for roughly 10 seconds before timing out. What I found is that the Test thread will acquire the lock and the program will hang for those 10 seconds. That indicates that the OtherThread thread can't acquire the lock because it is still owned by the Test thread. Once the 10 seconds are expired, the Test thread completes and releases the lock, which allows the other thread to finish. If I remove the 10,000 ms time-out, the program seems to hang indefinitely. I hope that helps, Corey