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).]
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.