Java

What is Synchronization in Java ? why it is useful for a programmer ?6 min read

In this article we will learn about what is synchronization? why synchronization is needed? and how to synchronize threads in Java along with sample programs.

Why Synchronization is Needed?

When two or more threads are accessing the same resource like a variable or a data structure, it may lead to inconsistent data or values. Such conditions that lead to inconsistency are known as race conditions.




As an example let’s consider a variable count whose value is 7 at present. Consider two operations: count = count + 1 which is executed by thread1 and another operation count = count – 1 which is executed by thread2. Note that both threads are sharing the common variable count.

If both threads execute in parallel then the sequence of operations can be either:

or

In both sequences the end value of count will be 7 which is a consistent value. But often a single high-level language statement will be converted to multiple assembly language statements.

The count = count + 1 will be converted to following assembly language statements:

Statements are labelled as A, B and C for convenience. R1 is a CPU register. Similarly count = count – 1 will be converted to following assembly language statements:

Again statements are labelled as D, E and F for convenience. R2 is another CPU register. Statements A, B and C are executed by thread1 in parallel with statements D, E and F of thread2.

Let the value of count be 7. Now consider the following statement execution sequence: A, B, D, E, C, F as shown below:

End value of count after the above execution sequence is 6 which is an inconsistent value and the execution sequence can be considered as an example that led to race condition.

To prevent race conditions we can use a mechanism known as synchronization.

What is Synchronization?

Synchronization is a mechanism which restricts simultaneous access to a shared resource by multiple threads.

Synchronization is used at large in operating systems like Windows, Linux, MacOSX etc. No synchronization might lead to a deadlock which is a serious problem. In Java, synchronization is supported by the synchronized keyword.

Using synchronized keyword we can create:

  1. Synchronized methods
  2. Synchronized blocks

Synchronization in Java

The key to synchronization in Java is monitor. A monitor is an object which is used for obtaining a mutual exclusive lock. Once a thread acquires a lock, it is said to have entered the monitor. When one thread is inside the monitor, no other thread is allowed to acquire a lock until that thread exits the monitor.

Every object in Java has an implicit monitor associated with it. To enter an object’s monitor, a method which is modified using synchronized keyword should be called.

Synchronized Methods

A method that is modified with the synchronized keyword is called a synchronized method. Syntax of a synchronized method is as follows:

First let’s consider a program which doesn’t contain the synchronized method:

Output of the above program is:

As you can observe in the above program, all three threads are executing the same method printName() simultaneously and hence the inconsistent output. For consistent output we can make the printName() method synchronized.

Below example demonstrates the use of synchronized methods in Java:

Output will be

In the above example synchronized method is printName() of MyName class.

Sometimes while working with third-party classes, we might want to synchronize the access to methods in those classes. In such cases, synchronized methods cannot be used. Instead we can use synchronized blocks. Syntax of a synchronized block is as follows:

The above program can be modified to contain a synchronized block as shown below:

Output of the above program is:

In the above program the call to the method printName() is written inside a synchronized block.

Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us…

Leave a Comment