Synchronization in Java
Synchronization in java is usedΒ to control the access of multiple threads to any shared resource.
It allows only one thread to access the shared resource.
Why use Synchronization
The synchronization is mainly used to
- prevent thread interference.
- prevent consistency problem.
There are two types of thread synchronization mutual exclusive and inter-thread communication.
- Mutual Exclusive
- Synchronized method.
- Synchronized block.
- static synchronization.
- Inter-thread communication in java
In java Synchronization can be done by either synchronized method or synchronized block and we use synchronized keyword for both ways.
What is Lock in Java
Every object has a lock or monitor associated with it. Therefore, when thread enters into the synchronized block or method then monitor is acquired by that thread which has entered into the blockΒ , and then release the lock when it’s done with that block.
Problem without Synchronization
In this example, there is no synchronization, so output is inconsistent. Let’s see the example:
Output: 5 100 10 200 15 300 20 400 25 500
Java synchronized method
If you declare any method as synchronized, it is known as synchronized method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.
//example of java synchronized method class Table { synchronized void printTable(int n) { //synchronized method for (int i = 1; i <= 5; i++) { System.out.println(n * i); try { Thread.sleep(400); } catch (Exception e) { System.out.println(e); } } } } class MyThread1 extends Thread { Table t; MyThread1(Table t) { this.t = t; } public void run() { t.printTable(5); } } class MyThread2 extends Thread { Table t; MyThread2(Table t) { this.t = t; } public void run() { t.printTable(100); } } public class TestSynchronization2 { public static void main(String args[]) { Table obj = new Table(); //only one object MyThread1 t1 = new MyThread1(obj); MyThread2 t2 = new MyThread2(obj); t1.start(); t2.start(); } }
10 15 20 25 100 200 300 400 500
Synchronized block in java
Synchronized block can be used to perform synchronization on any specific resource of the method.
Suppose you have 100 lines of code in your method, but you want to synchronize only 20 lines, you can use synchronized block.
If you put all the codes of the method in the synchronized block, it will work same as the synchronized method.
Points to remember for Synchronized block
- Synchronized block is used to lock an object for any shared resource.
- Scope of synchronized block is smaller than the method.
Syntax to use synchronized block
- synchronizedΒ (object)Β {
- Β Β //codeΒ blockΒ
- }
Important Points:
- At a time only one thread can access a synchronized method or block.
- If any static method is synchronized then at a time only one object’s one thread can access the method. This is also called class level synchronization.
[…] A synchronized block does not provide fairness to the multiple threads because any thread can acquire the object lock once previous existing lock is released We can achieve fairness within the Lock APIs by specifying the fairness property. It makes sure that longest waiting thread is given access to the lock. […]