1. Introduction
Synchronization in Java is a mechanism used to control multiple threads accessing shared resources.
It ensures that:
Only one thread executes a critical section at a time
Data remains consistent and accurate
Without synchronization, multithreaded programs can produce unexpected results.
2. Why Synchronization is Needed
Explanation
When multiple threads access and modify the same data simultaneously, it leads to problems like:
Race Condition
Data Inconsistency
Thread Interference
Synchronization solves these issues by controlling thread access.
3. Race Condition Example (Without Synchronization)
class Counter {
int count = 0;
void increment() {
count++;
}
}
public class Test {
public static void main(String[] args) {
Counter c = new Counter();
Runnable task = () -> {
for (int i = 0; i < 1000; i++) {
c.increment();
}
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
}
}
Explanation of Code
Two threads increment the same variable
countExpected result:
2000Actual result: unpredictable (less than 2000)
Why?
Because count++ is not atomic:
Read → Modify → Write (3 steps)
Threads interfere with each other
4. Synchronization Solution
class Counter {
int count = 0;
synchronized void increment() {
count++;
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
Runnable task = () -> {
for (int i = 0; i < 1000; i++) {
c.increment();
}
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(c.count);
}
}
Explanation of Code
synchronizedkeyword ensures:Only one thread can execute
increment()at a time
join()ensures main thread waits for others
Output:
2000
5. Types of Synchronization
1. Method Level Synchronization
synchronized void method() {
// critical section
}
Locks the entire method
2. Block Level Synchronization
void method() {
synchronized(this) {
// critical section
}
}
Locks only a specific block → better performance
3. Static Synchronization
synchronized static void method() {
}
Locks the class-level lock
6. How Synchronization Works Internally
Every object in Java has a monitor (lock)
When a thread enters a synchronized block:
It acquires the lock
Other threads must wait until:
Lock is released
7. Advantages
Prevents race conditions
Ensures data consistency
Provides thread safety
8. Disadvantages
Performance overhead (due to locking)
Can lead to deadlocks if not used carefully
9. When to Use Synchronization
Use synchronization when:
Multiple threads access shared data
At least one thread modifies the data
10. Summary
Synchronization controls thread access to shared resources
Prevents data inconsistency and race conditions
Achieved using
synchronizedkeywordEssential for multithreaded applications
Java Full Stack Developer Roadmap
To master multithreading and synchronization concepts:
👉 https://www.ashokit.in/java-full-stack-developer-roadmap
Promotional Content
Want to master Java Multithreading and Synchronization concepts?
.png)
No comments:
Post a Comment