Object level locking is synchronizing a block of code or whole method itself so that -
1) Different Threads on Different Instance of Object - Threads on different objects may enter the synchronized block of code at the same time. This means multiple objects of same class that exists in memory has their own individual lock.
2) Different Threads Same Instance of Object - Multiple threads on same object may exists in memory but only one thread on that object instance can enter synchronized block at a time. All other threads will be in wait state.
There are two flavors of Object lock
public void syncMethod() { synchronized (this) { } }
public synchronized void syncMethod() { }
Here is the implementation of Case 1 - Different Threads on Different Instance of Object
ObjectLevelLockingImpl.java - Thread Class
package com.learning.objectclasslocking; public class ObjectLevelLockingImpl implements Runnable { public void syncMethod() { synchronized (this) { 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; } }
ThreadsOnDifferentObjectLocking.java - This implementation is for Different Threads on Different Instance of Object - Threads on different objects may enter the synchronized block of code at the same time. This means multiple objects of same class that exists in memory has their own individual lock. In this class, 2 objects of class ObjectLevelLockingImpl are created and 2 threads are started at the same time. Observe the output, both the threads are able to enter the synchronized method at the same time.
package com.learning.objectclasslocking; public class ThreadsOnDifferentObjectLocking { public static void main(String args[]) { int count = 0; ObjectLevelLockingImpl objectLevelLockingOne = new ObjectLevelLockingImpl(); objectLevelLockingOne.setTime(4000); objectLevelLockingOne.setCount(++count); new Thread(objectLevelLockingOne).start(); System.out.println("First Thread Started"); ObjectLevelLockingImpl objectLevelLockingTwo = new ObjectLevelLockingImpl(); objectLevelLockingTwo.setTime(1000); objectLevelLockingTwo.setCount(++count); new Thread(objectLevelLockingTwo).start(); System.out.println("Second Thread Started"); System.out.println("Client ends"); } }
Output -
First Thread Started Second Thread Started Client ends Sync Method Starts 1 Sync Method Starts 2 Sync Method Ends 2 Sync Method Ends 1
ThreadsOnSameObjectLocking.java - Different Threads Same Instance of Object - Multiple threads on same object may exists in memory but only one thread on that object instance can enter synchronized block at a time. All other threads will be in wait state.
package com.learning.objectclasslocking; public class ThreadsOnSameObjectLocking { public static void main(String args[]) { int count = 0; ObjectLevelLockingImpl objectLevelLockingOne = new ObjectLevelLockingImpl(); objectLevelLockingOne.setTime(4000); objectLevelLockingOne.setCount(++count); Thread threadOne = new Thread(objectLevelLockingOne); threadOne.setName("Thread One"); Thread threadTwo = new Thread(objectLevelLockingOne); threadTwo.setName("Thread Two"); threadOne.start(); threadTwo.start(); System.out.println("Main Ends"); } }
Output -
Main Ends Sync Method Starts 1 Sync Method Ends 1 Sync Method Starts 1 Sync Method Ends 1
No comments:
Post a Comment