Class Level Locking In Java - Java @ Desk

Sunday, December 9, 2018

Class Level Locking In Java

Class Level Locking In Java

Class level locking is synchronizing a block of code at a class level or static method itself so that even if multiple threads exists on same or different objects of class but only one thread can enter into the synchronized block or static synchronized method.

It means if 10 instances of a class exists in JVM then only one thread can acquire class level lock. In case of static methods, the lock is always applied on class.

Class level locking is achieved using below 2 options -
public void syncMethod() {
        synchronized (ClassLevelLockingImpl.class) {
  }
 }
}


public static synchronized void syncMethod() {}


As soon as thread entered Synchronized block, thread acquired class object. Thread will leave lock when it exits synchronized block and only after this another waiting thread may enter the block.

Here is the implementation of Class level lock

ClassLevelLockingClient.java - In this implementation, 2 objects are created of Thread class that has class level lock implementation and both threads are started at the same time. Since the lock is applied on a lock, only 1 thread enters into the synchronized block and other enters in wait state
package com.learning.objectclasslocking;

public class ClassLevelLockingClient {

    public static void main(String args[]) {
        int count = 0;
        ClassLevelLockingImpl objectLevelLockingOne = new ClassLevelLockingImpl();
        objectLevelLockingOne.setTime(4000);
        objectLevelLockingOne.setCount(++count);
        new Thread(objectLevelLockingOne).start();

        ClassLevelLockingImpl objectLevelLockingTwo = new ClassLevelLockingImpl();
        objectLevelLockingTwo.setTime(1000);
        objectLevelLockingTwo.setCount(++count);
        new Thread(objectLevelLockingTwo).start();

        System.out.println("Client ends");
    }
}


ClassLevelLockingImpl.java - This is a thread class that has implemented Class level lock
package com.learning.objectclasslocking;

public class ClassLevelLockingImpl implements Runnable {

    public void syncMethod() {
        synchronized (ClassLevelLockingImpl.class) {
            System.out.println("Sync Method Starts " + count);
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Sync Method Ends " + count);
        }
    }

    public void run() {
        this.syncMethod();
    }

    private int count;

    private int time;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}


Output -
Client ends
Sync Method Starts 1
Sync Method Ends 1
Sync Method Starts 2
Sync Method Ends 2






No comments:

Post a Comment