Object level lock is used to add synchronization to threads created from the same object. On the other hand, Class level lock is used to add synchronization to threads created from all objects of a class.

Let us see each one of these locking mechanism one by one:

Object Level locking

package com.example;

public class Bar implements Runnable {

	@Override
	public void run() {
		objectLock();
	}
	
	public void objectLock() {
		System.out.println(Thread.currentThread().getName());
		synchronized(this) {
			System.out.println("synchronized block " + Thread.currentThread().getName());
			System.out.println("synchronized block " + Thread.currentThread().getName() + " end");
		}
	}

	public static void main(String[] args) {
		Bar b1 = new Bar();
		Thread t1 = new Thread(b1);
		Thread t2 = new Thread(b1);
		
		Bar b2 = new Bar();
		Thread t3 = new Thread(b2);
		
		t1.setName("t1");
		t2.setName("t2");
		t3.setName("t3");
		
		t1.start();
		t2.start();
		t3.start();
	}
}

Output:

t1
t3
t2
synchronized block t3
synchronized block t3 end
synchronized block t1
synchronized block t1 end
synchronized block t2
synchronized block t2 end

Note that the thread t3 will not block when threads t1 and t2 block. This is because the lock has been placed on “this” object and thread t3 has different “this” object than threads t1 or t2.

Class Level lock

The code in object level lock example is using this reference for locking. However we can replace it with a clazz object so that it becomes a class level lock. By using a Bar class level lock, all the threads created using any object of class Bar shall get blocked.

The source code with modified lock follows:

package com.example;

public class Bar implements Runnable {

	@Override
	public void run() {
		objectLock();
	}
	
	public void objectLock() {
		System.out.println(Thread.currentThread().getName());
		synchronized(Bar.class) {
			System.out.println("synchronized block " + Thread.currentThread().getName());
			System.out.println("synchronized block " + Thread.currentThread().getName() + " end");
		}
	}

	public static void main(String[] args) {
		Bar b1 = new Bar();
		Thread t1 = new Thread(b1);
		Thread t2 = new Thread(b1);
		
		Bar b2 = new Bar();
		Thread t3 = new Thread(b2);
		
		t1.setName("t1");
		t2.setName("t2");
		t3.setName("t3");
		
		t1.start();
		t2.start();
		t3.start();
	}
}

Output:

t1
t3
synchronized block t1
synchronized block t1 end
t2
synchronized block t2
synchronized block t2 end
synchronized block t3
synchronized block t3 end

The code reflects the change that this has been replaced with Bar.class level lock. Note that the order of output is generated from the two lines:

System.out.println("synchronized block " + Thread.currentThread().getName());
System.out.println("synchronized block " + Thread.currentThread().getName() + " end");

is going to be executed for the same thread which entered the synchronized block.

In the above code for class level lock, we can replace Bar.class with any other class level lock including Object.class, HashMap.class or ArrayList.class.

Related Posts

Object and Class level locks in Java admin Core Java
Object level lock is used to add synchronization to threads created from the same object. On the other hand, Class level lock is used to add synchronization to threads created from all objects of a class. Let us see each one of these locking mechanism one by one: Object Level...
Object level lock is used to add synchronization to threads created from the same object. On the other hand, Class level lock is used to add synchronization to threads created from all objects of a class. Let us see each one of these locking mechanism one by one: <h2>Object Level locking</h2> 1 <strong>Output:</strong> <blockquote> t1 t3 t2 synchronized block t3 synchronized block t3 end synchronized block t1 synchronized block t1 end synchronized block t2 synchronized block t2 end </blockquote> Note that the thread t3 will not block when threads t1 and t2 block. This is because the lock has been placed on "this" object and thread t3 has different "this" object than threads t1 or t2. <h2>Class Level lock</h2> The code in object level lock example is using this reference for locking. However we can replace it with a clazz object so that it becomes a class level lock. By using a Bar class level lock, all the threads created using any object of class Bar shall get blocked. The source code with modified lock follows: 1 <strong>Output:</strong> <blockquote> t1 t3 synchronized block t1 synchronized block t1 end t2 synchronized block t2 synchronized block t2 end synchronized block t3 synchronized block t3 end </blockquote> The code reflects the change that this has been replaced with Bar.class level lock. Note that the order of output is generated from the two lines: 1 is going to be executed for the same thread which entered the <a href="http://www.javaexperience.com/hashtable-collections-synchronizedmap-and-concurrenthashmap/" title="Difference between Hashtable, Collections.synchronizedMap and ConcurrentHashMap">synchronized</a> block. In the above code for class level lock, we can replace Bar.class with any other class level lock including Object.class, HashMap.class or ArrayList.class.
Difference between wait/notify mechanism and synchronized
Difference between Hashtable, Collections.synchronizedMap and ConcurrentHashMap
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments