Monday, 10 June 2019

Object-Level and Class-Level Locks in Java with Examples

         This is an important interview question for experienced Java developers, as it tests their understanding of the different types of locks used in multithreading.

As we know, Java supports the concurrent execution of multiple threads. When two or more threads access the same shared resource simultaneously, it can lead to data inconsistency or race conditions. To avoid these issues, Java provides the synchronization mechanism, which ensures that only one thread can access a shared resource at a time while other threads wait for their turn.

Synchronization is achieved using the synchronized keyword, which can be applied at either the method level or the block level.

There are two types of locking in Java multithreading:

  • Object level lock
  • Class level lock

Object level lock:-

         Object level locking is about synchronizing a non-static method or non-static code block such that only one thread will execute the code block on given instance of the class. Object level locking make instance level data thread safe.

There are different ways we can lock the object in thread as below,

public class ThreadClassEx {
      
       public synchronized void syncMethod(){
             //method implementation here
       }
}

public class ThreadClassEx {
      
       public void syncMethod(){
           
              synchronized(this) {
                  //block implementation here
              }
       }
}

public class ThreadClassEx {
      
         private final Object lock = new Object();

         public void syncMethod(){
           
                   synchronized(lock) {
                           //block implementation here
                   }
          }
}


Class level lock:-

         Class level locking is about a synchronizing a static method or block so that it can be accessed by only one thread for whole class. If you have 100 instances of class, only one thread will be able to access only one method or block of any one instance at a time.

        This should always be done to make static data thread safe.

Various ways of class level locking is as follows,

public class ThreadClassEx {

      public synchronized static void syncMethod(){
               //method implementation here
      }
}

public class ThreadClassEx {

      public void syncMethod(){
         
            synchronized(ThreadClassEx.class) {
               //block implementation here
            }
      }
}

public class ThreadClassEx {

      private final static Object lock = new Object();

      public void syncMethod(){
         
            synchronized(lock) {
               //block implementation here
            }
      }
}

Thank you for visiting blog.


Related Posts:-
1) Producer Consumer Problem - Solution using wait and notify In Java
2) Thread join() method example in Java
3) Difference between the Runnable and Callable interface in Java
4) Explain Thread life cycle and difference between wait() and sleep() method.
5) How many ways to create a thread in Java? Which one Prefer and why?
6) Producer Consumer Problem - Solution using BlockingQueue in Java

No comments:

Post a Comment