Java Class Level Synchronization

In this tutorial, we will learn about the concept of class level lock in Java and how it can be achieved along with the working examples of it.

class level synchronization

In Java, every class has a unique lock associated with it and this is called as class level lock. Class level lock is different from object level lock, whereas object level lock can be applied only on objects, method and variable which are not static but in case of class level lock can be applied only on static methods or static variables.

To achieve class-level synchronization, you can use the synchronized keyword on a static method or on a synchronized block on static variables. As class will be only one hence only one thread can get an access of these static synchronized blocks at a time per runtime.

Syntax:

private static synchronized void classLevelLockMethod() {
    //set of instructions to be written here
}

Example1:

CopiedCopy Code
public class ClassLevelLockingExample extends Thread {
	public ClassLevelLockingExample(String threadName) {
		super(threadName);
	}
	public static void main(String[] args) {
		Thread t1 = new ClassLevelLockingExample("Thread 1");
		Thread t2 = new ClassLevelLockingExample("Thread 2");
		t1.start();
		t2.start();
	}
	public void run() {
		ClassLevelLockingExample.classLevelLockMethod();
	}
	private static synchronized void classLevelLockMethod() {
		try {
			System.out.println(Thread.currentThread().getName() +": entered");
			Thread.sleep(1000);
			System.out.println(Thread.currentThread().getName() +": exited");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

Output:

Thread 1: entered
Thread 1: exited
Thread 2: entered
Thread 2: exited

Explanation:

  1. Created a class ClassLevelLockingExample which extends Thread class.
  2. Created a method with name classLevelLockMethod with static synchronized
  3. Created two new threads with the names "Thread 1" and "Thread 2"
  4. Started both the threads.
  5. Both threads entered the run method but only one thread will get an access at a time.
  6. As it is a class level, only one thread gets an access although no objects shared across two threads.
  7. Once "Thread 1" completes its execution then "Thread 2" enters and completes its execution.

Example2:

In this example, we will apply a class level lock on a static variable.

CopiedCopy Code
public class StaticVariableLock implements Runnable {
	private static Object staticVariable=new Object();
	public static void main(String[] args) {
		Thread t1 = new Thread(new StaticVariableLock(),"Thread T1");
		Thread t2 = new Thread(new StaticVariableLock(),"Thread T2");
		t1.start();
		t2.start();
	}
	private static void staticVariableMethod() {
		synchronized (StaticVariableLock.staticVariable) { //Applied lock on static variable
			try {
				System.out.println(Thread.currentThread().getName() +": entered");
				Thread.sleep(1000);// Goes for sleep of 1s
				System.out.println(Thread.currentThread().getName() +": exited");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public void run() {
		StaticVariableLock.staticVariableMethod();
	}
}

Output:

Thread T1: entered
Thread T1: exited
Thread T2: entered
Thread T2: exited

Explanation:

  1. In the above example, we have declared lock on static variable.
  2. Only one thread can access the synchronized static block.
  3. Hence, one thread entered and completed execution then second thread entered and completed execution.

Conclusion:

In this tutorial, we have covered what is class level lock including syntax along with the working examples of the class level lock.