Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

February 13, 2016 Categories: Multithreading. No Comments on Synchronization

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:

count = count + 1

count = count – 1

or

count = count – 1

count = count + 1

 

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:

A: R1 = count

B: R1 = R1 + 1

C: count = R1

 

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:

D: R2 = count

E: R2 = R2 – 1

F: count = R2

 

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:

A: R1 = count (R1 = 7)

B: R1 = R1 + 1 (R1 = 8)

D: R2 = count (R2 = 7)

E: R2 = R2 – 1 (R2 = 6)

C: count = R1 (count = 8)

F: count = R2 (count = 6)

 

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:

synchronized return-type method-name(parameters)

{ … }

 

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

 

Output of the above program is:

[My name [My name [My name is is is Suryateja]
Suryateja]
Suryateja]
Main thread terminated

 

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 of the above program is:

[My name is Suryateja]
[My name is Suryateja]
[My name is Suryateja]
Main thread terminated

 

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

 

Synchronized Blocks

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:

synchronized(object-reference)

{   //Statements to be synchronized }

 

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

 

Output of the above program is:

[My name is Suryateja]
[My name is Suryateja]
[My name is Suryateja]
Main thread terminated

 

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

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me