Synchronization is a modifier that is used for the method and blocks only. With the help of a synchronized modifier, we can restrict a shared resource to be accessed only by one thread at a time. When two or more threads need access to shared resources, there is some loss of data i.e. data inconsistency. The process by which we can achieve data consistency between multiple threads is called Synchronization.
Object-level lock: is a mechanism when we want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on a given instance of the class. This should always be done to make instance-level data thread-safe.
Approach: Using synchronized the method
Java
public class ObjectLevelLocking {public static void main(String[] args) {Employee e = new Employee();Thread t1 = new Thread(e);Employee e1 = new Employee();Thread t2 = new Thread(e1);t1.start();t2.start();}static class Employee implements Runnable {String name = "Rahul";public synchronized void showEmployee() {System.out.println(name + " " + Thread.currentThread().getName());}@Overridepublic void run() {showEmployee();}}}
Approach: Using synchronized the block
Java:
public class ObjectLevelLocking {public static void main(String[] args) {Employee e = new Employee();Thread t1 = new Thread(e);Employee e1 = new Employee();Thread t2 = new Thread(e1);t1.start();t2.start();}static class Employee implements Runnable {String name = "Rahul";public void showEmployee() {synchronized (this) {System.out.println(name + " " + Thread.currentThread().getName());}}@Overridepublic void run() {showEmployee();}}}
Class level lock: 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.
Class level lock prevents multiple threads to enter in
synchronized
block in any of all available instances of the class on runtime.Approach: Lock on .class reference
Java
public class ClassLevelLocking {public static void main(String[] args) {Employee e = new Employee();Thread t1 = new Thread(e);Employee e1 = new Employee();Thread t2 = new Thread(e1);t1.start();t2.start();}static class Employee implements Runnable {String name = "Rahul";public void showEmployee() {synchronized (Employee.class) {System.out.println(name + " " + Thread.currentThread().getName());}}@Overridepublic void run() {showEmployee();}}}
Approach: Using a method static
Java
public class ClassLevelLocking {public static void main(String[] args) {Employee e = new Employee();Thread t1 = new Thread(e);Employee e1 = new Employee();Thread t2 = new Thread(e1);t1.start();t2.start();}static class Employee implements Runnable {static String name = "Rahul";public synchronized static void showEmployee() {System.out.println(name + " " + Thread.currentThread().getName());}@Overridepublic void run() {showEmployee();}}}
Key Point to remember:
Object Level Lock:
- It can be used when you want a non-static method or non-static block of the code should be accessed by only one thread.
- t should always be used to make non-static data thread-safe.
- Every object in the class may have its own lock.
Class Level Lock:
- It can be used when we want to prevent multiple threads to enter the synchronized block in any of all available instances on runtime.
- It should always be used to make static data thread-safe.
- Multiple objects of class may exist but there is always one class’s class object lock available
No comments:
Post a Comment