9

Object level and Class level locks in Java with example

 2 years ago
source link: http://adnjavainterview.blogspot.com/2019/06/object-level-and-class-level-locks-in-java.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Object level and Class level locks in Java with example

        This question is also important for experienced java developers to know about type of locks in multithreading.         
        As we know that java supports multiple threads to be executed concurrently. If  two or more threads accessing the same resource at a time then conflict/inconsistent will occur. To avoid this inconsistency we are following synchronization concept i.e at a time one thread can access resource and wait for another thread to access. Using synchronized keyword to achieve the synchronization.  The synchronized keyword can be used as method level or block level.

There are two types of locking in java in threading,
  • 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
            }
      }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK