File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 2 q on threads: join and synchrnoized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "2 q on threads: join and synchrnoized block" Watch "2 q on threads: join and synchrnoized block" New topic

2 q on threads: join and synchrnoized block

Karel KoboojBot
Ranch Hand

Joined: Apr 09, 2004
Posts: 35
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:

Does thread release both A and B or only B ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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,

SCJP Tipline, etc.
I agree. Here's the link:
subject: 2 q on threads: join and synchrnoized block
It's not a secret anymore!