When we are working on multiple threads, a condition might arise in which they will need to access the same resource. Synchronization is the capability in Java to control the access of multiple threads if they try to access one shared resource.
Whenever we have multiple threads working in our environment and our objects are shared between them, we need to use synchronization so we can prevent scenarios like deadlocks. A deadlock is a state when a thread keeps on waiting for resources for a long time.
Using synchronization we can prevent thread interference. Also, we can prevent the concurrency problem.
There are two types of synchronization.
When multiple threads or processes get executed together to get a state where they can commit some sequence of actions.
There are two types of thread synchronization:
1. Mutual Exclusive
This type helps to make sure that only one thread can access the shared resource.
We can achieve this by using the synchronized method, synchronized block and static synchronization.
2. Cooperation Method
Inter-thread communication is used to achieve synchronization in Java. It is a method in which a thread will be stopped from its running state and will move to a blocked state. It can be allowed to come back to the critical section by some of the defined methods which can be called.
We implement synchronization using the keyword “synchronized” in Java. There is a locking technique that is used to achieve synchronization and it is implemented by Java Virtual Machine (JVM). The concept of a monitor is used during synchronization. Whenever an object is created, an object lock also gets created with it. Monitor is an object which is used as a mutual exclusive lock. Monitor can be owned by one thread only. To execute any block, a thread needs to acquire an object lock associated with that object. When a thread acquires the lock it is called entered monitor. Every other thread trying to enter inside a synchronized block or trying to acquire the object lock will be blocked or suspended until the first thread releases the object lock. Once a thread is running a synchronized method or blocks all other threads, it will be waiting for the monitor.
Synchronized method
1
2
3
4
5
class Solution {
synchronized void synchronizedMethod() {
// Set of Statements
}
}
1 2 3 4
Synchronized block synchronized { //set of statement }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
synchronized void sol(String message) {
System.out.println("[ " + message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
} // Sol method Ends
System.out.println("]");
} // Solution Class Ends
class CallerSolution implements Runnable {
String msg;
Solution target;
Thread t;
public CallerSolution(Solution tar, String s) {
target = tar;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
target.sol(msg);
}
} // CallerSolution Ends
class SynchMainClass {
public static void main(String args[]) {
Solution obj1 = new Solution();
CallerSolution c1 = new CallerSolution(obj1, "Hello");
CallerSolution c2 = new CallerSolution(obj1, "Synchronized");
CallerSolution c3 = new CallerSolution(obj1, "Method");
try {
c1.t.join();
c2.t.join();
c3.t.join();
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
Output:
[ Hello [Synchronized [ Method ] ] ]