Class Level Lock in Java

17 Jan, 2022
Every class in Java has a unique lock which is nothing but class level lock.  If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Class level lock prevents multiple threads to enter a synchronized block in any of all available instances of the class on runtime. This means if in runtime there are 10 instances of a class, only one thread will be able to access only one method or block of any one instance at a time. It is used if you want to protect static data.
If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Once a thread got the class level lock, then it is allowed to execute any static synchronized method of that class. Once method execution completes automatically thread releases the lock.
Methods: Thread can acquire the lock at a class level by two methods namely
Using the synchronized static method.Using synchronized block.
Using the synchronized static method.
Using synchronized block.
Method 1: Using the synchronized static method
Implementation: We have a Geek class. We want to use static synchronization method of this class, as soon as the thread entered the synchronized method, the thread acquires the lock at the class level, rest of the threads wait to get the class monitor lock. The thread will leave a lock when it exits from the synchronized method.
public static synchronized int incrementCount()
{
}
Example
Java
// Java program to illustrate class level lock // Main Class// Implememnting the Runnable interfaceclass Geek implements Runnable {     // Method 1    // @Override    public void run() { Lock(); }     // Method 2    // Method is static    public static synchronized void Lock()    {        // Gwetting the name of current thread by using        // getName() method to get name of the thread and        // currentThread() to get the current thread        System.out.println(            Thread.currentThread().getName());         // class level lock        synchronized (Geek.class)        {            System.out.println(                "in block "                + Thread.currentThread().getName());            System.out.println(                "in block "                + Thread.currentThread().getName()                + " end");        }    }     // Method 3    // Main driver method    public static void main(String[] args)    {        // Creating an object of above class        // in the main() method        Geek g1 = new Geek();         // Sharing the same object across two Threads         // Creating an object of thread class where        // t1 takes g1        Thread t1 = new Thread(g1);         // Creating an object of thread class where        // t2 takes g1        Thread t2 = new Thread(g1);         // Creating second object of above class        // in the main() method        Geek g2 = new Geek();         // Creating an object of thread class where        // t3 takes g2        Thread t3 = new Thread(g2);         // setName() method is used to set name to the        // thread        t1.setName("t1");        t2.setName("t2");        t3.setName("t3");         // start() method is used for initiating the current        // thread        t1.start();        t2.start();        t3.start();    }}
  
t1
in block t1
in block t1 end
t3
in block t3
in block t3 end
t2
in block t2
in block t2 end
 
Output explanation: 
 
Thread t1 entered the static synchronized method and was holding a lock on Geek’s class. So, the rest of the threads waited for Thread t1 to release the lock on Geek’s class so that it could enter the static synchronized method.
 
Method 2: Using synchronized block method 
 
Implementation: We have a “Geek” class. We want to create a synchronization block and pass class name. class as a parameter tells which class has to synchronized at the class level. As soon as the thread entered the synchronized block, the thread acquire the lock at class, rest of the threads wait to get the class monitor lock. The thread will leave lock when it exits from the synchronized block.
 
synchronized (Geek.class) {
    //thread has acquired lock on  Geek class
}
 
Example
 
Java
// Java program to illustrate class level lock // Main Class// It is implementing the Runnable interfaceclass Geek implements Runnable {     // Method 1    // @Override    public void run()    {        // Acquire lock on .class reference        synchronized (Geek.class)         // ClassName is name of the class containing method.        {            {                System.out.println(                    Thread.currentThread().getName());                 System.out.println(                    "in block "                    + Thread.currentThread().getName());                System.out.println(                    "in block "                    + Thread.currentThread().getName()                    + " end");            }        }         // Method 2        // Main driver method        public static void main(String[] args)        {            // Creating an object of above class            // in the main() method            Geek g1 = new Geek();             // Creating an object of thread class i.e Thread            // 1 where t1 takes g1 object            Thread t1 = new Thread(g1);            // Here, creating Thread 2 where t2 takes g1            // object            Thread t2 = new Thread(g1);             // Creating another object of above class            // in the main() method            Geek g2 = new Geek();                       // Now Creating Thread 3 where t3 takes g2 object            Thread t3 = new Thread(g2);             // Ginving custom names to above 3 threads            // using the setName() method            t1.setName("t1");            t2.setName("t2");            t3.setName("t3");             // start() method is used to begin execution of            // threads            t1.start();            t2.start();            t3.start();        }    }
 
 
Output:
 
t1
in block t1
in block t1 end
t3
in block t3
in block t3 end
t2
in block t2
in block t2 end
 
Output explanation: 
 
Thread t1 entered synchronized block and was holding the lock on ‘Geek’ class. So, the rest of the threads waited for Thread t1 to release the lock on the ‘Geek’ class so that it could enter the synchronized block.
 
kk773572498
Java-Class and Object
Java-Multithreading
Picked
Java
Java-Class and Object
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.