Synchronization in Java is the capability to control the access of multiple threads to any shared resource.Java Synchronization is a better option where we want to allow only one thread to access the shared resource.
Why use Synchronization?
The synchronization is mainly used to
- To prevent thread interference.
- To prevent consistency problem.
Types of Synchronization:
There are two types of synchronization
- Process Synchronization
- Thread Synchronization
Here, we will discuss only thread synchronization.
Understanding the problem without Synchronization
In this example, there is no synchronization, so the output is inconsistent. Let’s see the example:
5 100 10 200 15 300 20 400 25 500
Java synchronized method
If you declare any method as synchronized, it is known as 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.
Inter-thread communication in Java
Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other.
Cooperation (Inter-thread communication) is a mechanism in which a thread is paused 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:
It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls
wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the
wait() method. The
wait() method is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism.
public final void wait(long timeout) throws InterruptedException
Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object’s monitor. A thread becomes the owner of the object’s monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a
synchronizedstatement that synchronizes on the object.
- For objects of type
Class,by executing a synchronized static method of that class.
public final void notify()
Wakes up all threads that are waiting on this object’s monitor. A thread waits on an object’s monitor by calling one of the
wait methods.The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
public final void notifyAll()
Example of inter thread communication in java
going to withdraw... Less balance; waiting for deposit... going to deposit... deposit completed... withdraw completed