Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Locks  RSS feed

 
Divakar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am totally confused about locks(in Thread ).
Also I have seen in one of the postings the following.
(http://www.javaranch.com/ubb/Forum24/HTML/000643.html)
" a lock can be obtained using wait() method. But, wait is a method of Object class and thus every object can use wait() method ".
How one will get lock by calling wait() method??
I've read that wait() is to make the thread wait until another
thread completes execution and calls notify().
Also what is the difference between lock on method and lock on instance??
Can anyone clarify the concept of locks??
 
PGautam
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lock is variable of Object class, it has nothing do with Thread class. Thread has to obtain lock on a instance of object.
Object will enforce locking mechanism only if it contains synchronized method's. Also, wait() and notify() are object class methods, they can be used in only Sychronized methods.
If you have access to RHE (Robert, Heller, Ernest) certification guide, it has good discussion about locks(in chapter 7).Also, Exam cram book is good.
Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Threads and Synchronization.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divakar- you can't obtain a lock using wait() - instead, you are required to already have a lock on an object in order to use that object's wait() method. I overlooked this line in the post you quoted, or I would have corrected it then.
As for the difference between locks on methods and locks on instances - locks on methods don't exist (as I pointed out in the post you read). Locks on instances and locks on classes do exist, and they are all you need to worry about.
In addition to the books recommended by PGautam, there are several online tutorials you can look at. Marcus Green covers wait() and notify() here, and the Java Tutorial has it here. You can find related topics by following links from these locations.
 
Divakar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim,Gautum
Let me put what I understood.
Actually I was thinking synchronized methods will automatically be locked(ie,other threads can't access the object which contains the synchronized block or method,am I right??).wait()
and notify() are for informing the threads about the status of execution.
My idea is right or wrong???
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!