In this article Synchronization methods in multithreading we give the information about Synchronization is used during multithreading in Java to ensure that multiple threads do not access the same resource (such as a shared object) simultaneously, which can lead to data inconsistency.
Synchronization Methods in Multithreading (Java)
Introduction
Synchronization in Java ensures that multiple threads do not access a shared resource (like an object or variable) at the same time, preventing data inconsistency and ensuring thread safety.
It allows only one thread to enter the critical section at a time.
Main Synchronization Methods
Java provides three main methods for inter-thread communication:
| Method | Description | Belongs To |
| wait() | Makes the current thread wait until another thread invokes notify() or notifyAll(). | Object class |
| notify() | Wakes up one waiting thread that is waiting on the same object. | Object class |
| notifyAll() | Wakes up all threads that are waiting on the same object. | Object class |
Note: These methods must be called inside a synchronized block or method.
Synchronization in Java
When multiple threads access shared resources, synchronization ensures that only one thread can execute the critical section at a time.
You can use the synchronized keyword in two ways:
1. Synchronized Method
public synchronized void myMethod() { // Critical section code}
This ensures that only one thread can execute this method on a given object at a time.
2. Synchronized Block
public void myMethod() { synchronized(this) { // Critical section code }}
This allows finer control — only the enclosed code is synchronized, not the entire method.
Inter-thread Communication Methods
wait()
- Causes the current thread to wait until another thread invokes notify() or notifyAll().
- Releases the lock it holds on the object.
- Must be called inside a synchronized block/method.
Example:
synchronized(this) { wait(); // Thread goes into waiting state}
notify()
- Wakes up one waiting thread that is waiting on the same object.
- If multiple threads are waiting, only one random thread is resumed.
- Must be called inside a synchronized block/method.
Example:
synchronized(this) { notify(); // Wakes up one waiting thread}
notifyAll()
- Wakes up all threads waiting on the same object.
- Only one of them gets the lock and continues execution.
Example:
synchronized(this) { notifyAll(); // Wakes up all waiting threads}
Example: Using wait(), notify()
class SharedResource
{ synchronized void waitForTask()
{
try
{
System.out.println(Thread.currentThread().getName() + ” waiting…”);
wait(); // Thread will wait here
System.out.println(Thread.currentThread().getName() + ” resumed…”);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
synchronized void completeTask()
{
System.out.println(Thread.currentThread().getName() + ” notifying…”);
notify(); // Wakes up a waiting thread
}
}
public class WaitNotifyExample
{
public static void main(String[] args)
{
SharedResource resource = new SharedResource(); // Thread 1
new Thread(() ->
{
resource.waitForTask();
},
“Thread 1”).start(); // Thread 2
new Thread(() ->
{
try
{
Thread.sleep(1000); // Delay Thread 2 slightly
resource.completeTask();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}, “Thread 2”).start();
}
}
Explanation
- Thread 1 calls wait() and pauses execution.
- Thread 2 calls notify() after 1 second.
- notify() wakes up Thread 1, which resumes execution.
Summary
| Method | Function |
| Synchronization | Ensures only one thread accesses a shared resource at a time. |
| wait() | Pauses a thread until another thread calls notify() or notifyAll(). |
| notify() | Wakes up one waiting thread. |
| notifyAll() | Wakes up all waiting threads (only one proceeds). |
Conclusion
Synchronization in Java ensures thread safety by preventing simultaneous access to shared resources.
The methods wait(), notify(), and notifyAll() enable efficient inter-thread communication by managing thread coordination and execution flow.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining