What is Synchronization in Java?
In Java we can create multiple threads within a program. These threads may try to access the same resource which might lead to concurrency issue. for eg: Multiple threads might try to write within the same file then it may lead to corruption of data as one thread may override the data, while other thread may open it and at the same time another thread may close the file.
To avoid this situation there is a need to synchronize these threads such that at a given point of time only one thread can access the resource. This is implemented using the concept of lock or monitor. Every object in Java is associated with a monitor which a thread can lock or unlock. The thread that needs access to an object’s field acquires the lock and then releases it when it’s done with them.
Types of Synchronization
- Process Synchronization– Synchronization between programs is managed by process synchronization. For example, programs such as `Microsoft Excel` and `Microsoft PowerPoint` run as individual processes.
- Thread Synchronization–The concurrent execution of the critical resource by two or more Threads is termed Thread Synchronization. It can be further categorized to:-
- Mutual Exclusive
- Synchronized method.
- Synchronized block.
- Static synchronization.
- Cooperation (Inter-thread communication in Java)
- Mutual Exclusive
**Synchronized keywords cannot be used with class and variables.
Synchronized method
A method can be declared synchronized by using a synchronized keyword. The synchronized method is used to lock an object for any shared resource. If a thread invokes a method that is synchronized it gets the lock for that object and it releases the lock when it has finished its task.
Syntax:
Access modifiers synchronized return_type method_name (Method_Parameters)
{ }
Synchronized block
If you want to synchronize a few lines of code instead of synchronizing the entire method then we use synchronized blocks. The Synchronized block will synchronize only the code written in it. Synchronized block locks are accessed on the object, unlike Synchronized method locks that are accessed on the method.
Syntax:
synchronized (object reference expression) { //code block
}
Static synchronization
When a synchronized keyword is used with static methods then it is called Static synchronization . In this scenario, the lock will be on the class not on the object.
Syntax :-
synchronized static return type method name()
{//code}
Cooperation (Inter-thread communication in Java)
Inter-thread communication allows synchronized threads to communicate with each other.
In Inter-thread communication, a thread is paused that is running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed. It is implemented by following methods of Object class:
- wait() -It causes the current thread to wait until another thread invokes it.
- notify().The current thread releases the lock. This method is overloaded.
-
Method Description public final void wait()throws InterruptedException It waits until the object is notified. public final void wait(long timeout)throws InterruptedException It waits for a specified amount of time. - notify() – Wakes up a single thread that is waiting on this object’s monitor. If multiple threads are waiting on this object, then anyone arbitrary thread is awakened.
public final void notify()
- notifyAll()-Wakes up all threads that are waiting on this object’s monitor.
Syntax: public final void notifyAll()
Advantages of Synchronization Mechanism
- Thread Safety: It ensures thread safety as only one thread can access the shared resource at a time.
- Consistency: Because of synchronization the shared data is consistent throughout the program.
- Deadlock Prevention: Synchronization avoids deadlocks in a multi-threaded environment.
Disadvantages of Synchronization Mechanism
- Performance Overhead: As synchronization makes the thread wait for access to a shared resource the overall performance can decrease.
- Deadlocks: If not used correctly synchronization can lead to deadlocks.