Synchronization in Java: Managing Multi-threaded Access to Shared Resources
Understand how synchronization in Java allows control over multi-threaded access to shared resources, ensuring that only one thread can interact with a resource at a time. Learn why synchronization is essential in Java for managing thread safety and data integrity.
Synchronization in Java
Synchronization in Java is the capability to control the access of multiple threads to any shared resource. It is a better option when we want to allow only one thread to access the shared resource.
Why Use Synchronization?
Synchronization is mainly used to:
- Prevent thread interference.
- Prevent consistency problems.
Types of Synchronization
There are two types of synchronization:
- Process Synchronization
- Thread Synchronization
Here, we will discuss only thread synchronization.
Thread Synchronization
There are two types of thread synchronization: mutual exclusive and inter-thread communication.
Mutual Exclusive
- Synchronized Method
- Synchronized Block
- Static Synchronization
Inter-thread Communication
Mutual Exclusive
Mutual exclusive helps keep threads from interfering with one another while sharing data. It can be achieved by using the following three ways:
- By Using Synchronized Method
- By Using Synchronized Block
- By Using Static Synchronization
Concept of Lock in Java
Synchronization is built around an internal entity known as the lock or monitor. Every object has a lock associated with it. By convention, a thread that needs consistent access to an object's fields has to acquire the object's lock before accessing them and then release the lock when it's done with them.
From Java 5, the package java.util.concurrent.locks
contains several lock implementations.
Understanding the Problem without Synchronization
In this example, there is no synchronization, so the output is inconsistent. Let's see the example:
FileName: TestSynchronization1.java
class Table {
void printTable(int n) { // method not synchronized
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);
}
}
class TestSynchronization1 {
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();
}
}
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 a 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.
FileName: TestSynchronization2.java
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();
}
}
Output
5
10
15
20
25
100
200
300
400
500
Example of Synchronized Method Using Anonymous Class
In this program, we have created the two threads by using the anonymous class, so less coding is required.
FileName: TestSynchronization3.java
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);
}
}
}
}
public class TestSynchronization3 {
public static void main(String args[]) {
final Table obj = new Table(); // only one object
Thread t1 = new Thread() {
public void run() {
obj.printTable(5);
}
};
Thread t2 = new Thread() {
public void run() {
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}
Output
5
10
15
20
25
100
200
300
400
500