Thread Synchronization in Java

When two or more threads try to access the same resource, they need somehow to ensure that the resource will be used by only one thread at a time.

The process by which this achive is called Synchronization. Java uses the concept of monitor (also called semaphore) for Synchronization.

The monitor allows one thread at a time to execute a Synchronized method. Only one thread at a time may hold a lock on a monitor.

For a thread can lock or unlock each object in Java is associated with a monitor.

Example: Write a program to synchronize a thread.

In the above program, nothing exist to stop all three threads from entering write () method while other thread is using it.

These three threads are racing each other to complete the method and this condition is known as a race condition.

To fix this program, we must serialize access to write () method and restrict its access to only one thread at a time.

To do this, we simply need to write synchronized keyword before write () method definition. It is known as a synchronized method.

Synchronized Method

above method write() can be changed as below

Synchronized block: this technique is also used for thread synchronization

using synchronization block we can change write method as below

This prevent other threads from entering in write () method while other thread is using it. After synchronized has been added to write (), the output of the program.

Output: