Object locking


Here see some points...
  • Every object created by the JVM will have a lock
  • This lock will be unable whenever this object is used along with synchronized block/ synchronized methods 
  •   When non- synchronized  statements are in execution then identity of the lock will be disable 
  •   When you are invoking any instance method then object which is used to invoke  the method will be locked 
  •   When you are invoking static method then the default object(java.lang.class)will be locked.
Wait()        notify()        notifyAll()
Wait() and notify() are allowed to call inside Synchronized method or block only
When you call wait and notify() method outside the non-synchronized method or block, then the following Exception will be thrown :------------
                            Java.lang.IllegalMonitorStateException : current thread not owner.
 Why wait() and notify() method are provided in object class only why not in thread class?
Before to java 2 there is two method called suspend and resume in thread class. whose functionality is similar to wait() and notify().
Because of performance over head suspend and resume are deprecated and as              wait and notify method are provided in object class.
Because of methods of object class wait() and notify() method can be used in any java class without thread class object.

0 comments :

Post a Comment