Object level Locking vs. Class level Locking in Java

Synchronization is ability to restrict access to shared resource to only one thread. When two or more threads need access to shared resource, there has to be some mechanism such that shared resource will be used by only one thread. The process by which we can achieve it is called Synchronization.

In Java Synchronization is a modifier which is used for method and block only. With the help of synchronized modifier we can restrict a shared resource to be accessed only by one thread. When two or more threads need access to shared resources, there is some loss of data i.e. data inconsistency. The process by which we can achieve data consistency between multiple threads it is called Synchronization.

lock
            Locking

Using synchronized modified there are two types of lock can  be achieved –

  1.  Object Level Locking
  2.  Class Level Locking

Object level locking

Every object in java has a unique lock. Whenever we are using synchronized keyword, then only lock concept will come in the picture. If a thread wants to execute synchronized method on the given object. First, it has to get lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care by JVM and programmer is not responsible for these activities.

Object level locking is mechanism when you want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on given instance of the class.Lets have a look on the below program to understand the object level lock:

1
2
3
4
synchronized (this)
{
//other thread safe code
}

OR

1
2
3
4
5
6
7
8
9
10
11
class Test{

private final Object lock = new Object();
public void nonStaticMethod(){
synchronized (lock)
{
//other thread safe code
}
}
}
}

Class Level Lock

Synchronization is ability to restrict access to shared resource to only one thread. When two or more threads need access to shared resource, there has to be some mechanism such that shared resource will be used by only one thread. The process by which we can achieve it is called Synchronization.

In Java Synchronization is a modifier which is used for method and block only. With the help of synchronized modifier we can restrict a shared resource to be accessed only by one thread. When two or more threads need access to shared resources, there is some loss of data i.e. data inconsistency. The process by which we can achieve data consistency between multiple threads it is called Synchronization.

Using synchronized modified there are two types of lock can  be achieved 1: Object Level Locking , 2: Class Level Locking

Object level locking
Every object in java has a unique lock. Whenever we are using synchronized keyword, then only lock concept will come in the picture. If a thread wants to execute synchronized method on the given object. First, it has to get lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care by JVM and programmer is not responsible for these activities.

Object level locking is mechanism when you want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on given instance of the class.Lets have a look on the below program to understand the object level lock:

1
2
3
4
synchronized (this)
{
//other thread safe code
}

OR

1
2
3
4
5
6
7
8
9
10
11
class Test{

private final Object lock = new Object();
public void nonStaticMethod(){
synchronized (lock)
{
//other thread safe code
}
}
}
}

Class Level Locking

Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime. This means if at runtime there are n instances of TestClass, then only one thread will be able to execute demoMethod() in any one of instance at a time, and all other instances will be locked for other threads. This should always be done to make static data thread safe.

 

1
2
3
4
public class TestClass
{
public synchronized static void demoMethod(){}
}

or

1
2
3
4
5
6
7
8
9
public class TestClass
{
public void demoMethod(){
synchronized (TestClass.class)
{
//other thread safe code
}
}
}

or

1
2
3
4
5
6
7
8
9
10
public class TestClass
{
private final static Object lock = new Object();
public void demoMethod(){
synchronized (lock)
{
//other thread safe code
}
}
}

To Keep in mind !

  • java synchronized keyword is re-entrant in nature it means if a java synchronized method calls another synchronized method which requires same lock then current thread which is holding lock can enter into that method without acquiring lock.
  • Java Synchronization will throw NullPointerException if object used in java synchronized block is null. For example, in above code sample if lock is initialized as null, the synchronized (lock) will throw NullPointerException.
  • Do not synchronize on non final field on synchronized block in Java. because reference of non final field may change any time and then different thread might synchronizing on different objects i.e. no synchronization at all. Best is to use String class, which is already immutable and declared final.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *