Showing posts with label synchronization. Show all posts
Showing posts with label synchronization. Show all posts

Thursday, April 24, 2014

Synchronization in Java : class and object locking concept

People talk about two types of multi-threaded locking - object and class. In my knowledge, locking is done on objects only. Let me explain.

Case 1: On objects we create using new or factory methods etc.
void synchronized myMethod(Type param) {
  //will lock on the instance used to call this method
}
or
synchronized(this) {
 //will lock on current object
}
or
synchronized(obj1) {
 //will lock on specified obj1 object
}