What is Synchronization in Java? Explain synchronized keyword.

If multiple threads try to operate on a single Java object simultaneously, it may result in data inconsistency problem (Race Condition). Synchronization in java is the capability to control the simultaneous access of multiple threads operating on the shared resource. Java Synchronization is better option where we want to allow only one thread to access the shared resource at a given point of time.

Shared Resource
The shared resource is typically just a piece of memory in the form of an object, but may also be a file, I/O port, or something like a printer. To control access to a shared resource, first put it inside an object.

synchronized keyword can be used to achieve Synchronization in Java which can be applied on methods or blocks. Internally, Synchronization concept is implemented by using Locks. Every object in Java has an intrinsic lock. Whenever we use synchronized keyword, lock concept will come into the picture and JVM is responsible for acquiring or releasing lock.

Note: While a thread executing synchronized method on the given object, remaining threads are not allowed to execute simultaneously any synchronized method in that object. Non-synchronized methods can be executed on the same object by any number of threads simultaneously.

Case Study: If 2 different threads are operating on 2 different objects then, synchronization is not required.

Type of Locks in Java
Object Level Lock
Class Level Lock

If a method declared as static synchronized then, thread acquires class level lock. In Java, Locks can be acquired at Object or at Class which are known as Object level lock and Class level lock respectively.

If only few lines of code require synchronization then entire method is not needed to be synchronized, use synchronized block instead. The main advantage of synchronized block over synchronized method is, it reduces waiting time of threads and improves performance of the application.

public synchronized void display(String str) {
	// Code inside synchronized method
}
public void display(String str) {
	// Code before synchronized block
	synchronized (this) {
		// Code inside synchronized block
	}
	// Code after synchronized block
}
public void display(String str) {
	// Code before synchronized block
	synchronized (x) {
		// Code inside synchronized block
	}
	// Code after synchronized block
}
public void display(String str) {
	// Code before synchronized block
	synchronized (Main.class) {
		// Code inside synchronized block
	}
	// Code after synchronized block
}

Advantage: Data inconsistency issue can be resolved using synchronized keyword.
Disadvantage: It increases waiting time for threads and creates performance overhead. If there is no specific requirement, it is recommended not to use synchronized keyword.

Example: Ticket booking system, Banking transactions etc.

Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.