Explain the Significance of Synchronized Keyword

Here is another favorite interview question for Java Interviews – Explain the significance of synchronized keyword. The wordings of the question may vary but the essence remain same.

Before understanding synchronized keyword we must understand few things about Java’s multi threading.

Defining a Thread

A thread is an Operating System concept. A process under execution is called a thread or we can also say that a thread is a light weight process. Now the Operating System may manage the processor in such a way that it appears that a processor can in parallel accomplish multiple tasks.

Hence, it will appear to the program that the Operating System supports multi threading (execution of multiple processes at the same time).

Java in turn provides an abstraction over the Operating System Thread, which is represented by the class java.lang.Thread . As the OS supports multi threading, Java models it the same way.

The problems with multi threading

In a multi threading environment all these threads independently execute code that operate on values and objects residing in a shared memory. Now there is a chance that a thread might change an object or value and it is not communicated to the other thread which was operating on the value.

There can be many similar situations which may arise due to the lack of communication between threads.

Synchronization

Now that we know, the main problem being communication between threads.

The Java Programming Language provides multiple mechanism for communicating between threads. The most basic of these methods being synchronization, which is implemented using monitors.

Defining Monitor

Each object in Java is associated with a monitor, which can be locked or unlocked by a thread. You can consider the monitor as a resource which has the following property

  1. It can be locked by a thread. When the thread locks the monitor, it is said that the thread has acquired the lock of the monitor.
  2. Once a lock to the monitor is acquired by a thread, no other thread can acquire the lock on the same monitor.
  3. If a thread has acquired a lock to the monitor, it can lock the monitor multiple times, and every time it unlocks the monitor if affects into reversing one lock operation.

Synchronized Statements – Significance of Synchronized Keyword

Synchronized statements can be written as below:

The synchronized statement requires an object reference, it then tries to acquire the lock on the monitor of this object reference. It does not proceed until the lock is acquired. Once the lock is acquired, the body of the synchronized statement executes.

If execution of the body is completed, either normally or abruptly an unlock action is performed on the same monitor.

In method syncTest_1(), the object reference is the reference of the instance of the class, where as in method syncTest_2() the object reference is the instance variable mtr.

Synchronized Methods

Apart from synchronization using synchronized blocks, Java also supports synchronization using synchronized methods. A synchronized method is written as below:

The method when invoked, automatically tries to acquire a lock on the monitor of the object referenced by this keyword. The execution of the body of the method starts only when the lock action is successful. The lock is acquired on the monitor of the object which invoked the method (basically the object referenced by the this keyword).

If execution of the body is completed, either normally or abruptly an unlock action is performed on the same monitor.

Another variant of the synchronized methods are the static synchronized method which is written as below:

The method is always invoked using the class name.  It automatically tries to acquire a lock on the monitor of the object which refers to the Class object that represents the class where the static method belongs.. The execution of the body of the method starts only when the lock action is successful.

In this case, if the class name is TestSynchronization, then the lock would be acquired on an object of type java.lang.Class represented by the name TestSynchronization.

If execution of the body is completed, either normally or abruptly an unlock action is performed on the same monitor.

Conclusion

Hence, synchronized keyword helps in synchronizing the communication between two threads. As the lock of the monitor can be held only by one thread at a time, the risk of two threads modifying a shared resource (which is locked) is eliminated.

Stay subscribed for more formal and easy to understand and explain answers for Java Interviews.