• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread locks  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is from RHE;
How many locks does an object have?
1.
But every object can have class level lock and instance
lock.So, doesnt it make 2 locks.
Correct me where i am wrong?
2)A monitor is an instance of any class that has synchronized code. True/fals
This is true for instance methods.But
for class locks the monitor need not be
instance of any class.
Thanks!

[This message has been edited by avn (edited August 25, 2000).]
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avn,
It is recommended that you post all thread and synchronization questions in the Threads forum.
To answer your question let us define an Object. An Object is an instance of a Class. Thus the Class and its Object are two different entities. The question specifically asked is how many locks does an Object have and since each instance of Object and its subclasses possesses a lock, there is only one lock associated per Object.
However there is also another lock, the Class Lock. The class lock is not associated with a Object but on the Class.
Note:
What object does non-static synchronized methods use for locking?
Non-static synchronized methods synchronize on the instance (this) of the class.
What object does static synchronized methods use for locking?
Static synchronized methods synchronize on the class object (this.getClass()) of the class.

------------------
Regds.
Rahul P. Mahindrakar
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this to Threads Forum
Ajith
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking out for how the Class locks are implemented. I have the answer here
"Class locks are actually implemented as object locks. When the JVM loads a class file, it creates an instance of class java.lang.Class. When you lock a class, you are actually locking that class's Class object"


------------------
Regds.
Rahul P. Mahindrakar
 
Manoj Prithiani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a point of time the blocked thread which has locked a particular object (Class level lock) can lock all instances of it
But if it is instance level lock than it can be 'n' number
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!