Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About lock/unlock

 
Simon Li
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.
HashMap theLockRecord = new HashMap();
When I use a collection in lock/unlock, what the diference is between theLockRecord.wait() and wait() ?
It seems that we want to change the thread state to wait, do the two ways get the same result?
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two ways are different. When you call wait on the collection, you are asking the thread acessing the Collection object to wait.
When you call wait on the object(let us call it A) which holds the Collection, you are asking the thread which is trying to access the object A to wait.
 
Simon Li
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sai Prasad for your reply.
If I use
Hashtable theLockRecord = new Hashtable();
whether I don't have to use Synchronized(theLockRecord), because JDK says Hashtable is synchronized?
[ April 21, 2002: Message edited by: Simon Li ]
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first option, you are releasing the lock on the lockedRecord object monitor to other threads. In the second option, you are releasing the lock on the monitor for the object holding the lockedRecord.
Did you try to compile the option 2? I think you can call wait on the current object as long as the wait() is inside the synchronized block for the current object and not the lockedRecord sync block. I could be wrong. But I like to know what you find out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic