This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 q on threads: join and synchrnoized block

 
Karel KoboojBot
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Does thread release both A and B or only B ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic