Last Updated: 26 January, 2023
In a multithreaded environment, it is possible that more than one thread can access the same shared resources (i.e., variables) at a time, but this process creates a data consistency problem. To overcome this problem, Java has provided a feature called Synchronization.
Synchronization is a mechanism to control the access of multiple threads to any shared resource.
There are two types of synchronization supported by Java.
Process Synchronization: When multiple threads are executed simultaneously, process synchronization ensures that they reach a particular state and agree to a certain set of actions.
Thread Synchronization: Thread Synchronization ensures that at a time, only one thread can access the resource if multiple threads want to access the same resource at the same time.
In this article, we are going to discuss all about thread synchronization in Java and how to achieve it through Java programming.
Let's first understand the problem without synchronization in Java with the help of the given example.
Example 1 : Multithreading program without Synchronization
Output
Thread No. 1 : 2
Thread No. 1 : 4
Thread No. 2 : 2
Thread No. 2 : 4
Thread No. 1 : 6
Thread No. 1 : 8
Thread No. 1 : 10
Thread No. 2 : 6
Thread No. 2 : 8
Thread No. 2 : 10
After running the above program, we can see both the threads running at the same time and producing inconsistent output.
We can resolve such kind of problem using synchronization in Java.
We can achieve synchronization in Java by using the synchronized keyword. This keyword can be used with a method or a block. A synchronized method or block can be accessed by only one thread at a time, if there are multiple threads trying to access the same synchronized method or block then other threads will have to wait for the execution of the method by one thread. Synchronized keyword provides a lock on the object and thus prevents the race condition.
The synchronized keyword can be used with —
The synchronized block is used when we want to synchronize only some specific lines of code, not the entire method. The scope of the synchronized block is smaller than the method. The synchronized block is more preferred than the synchronized method in Java.
Syntax of synchronized block
Here, lockObject is a reference to an object whose lock is associated with the monitor that the synchronized statements represent.
We can write synchronized blocks inside an instance method as well as a static method. See the Java program implementation below.
Example 2 : Synchronized Block in Instance Method
Output
Thread No. 1 : 2
Thread No. 1 : 4
Thread No. 1 : 6
Thread No. 1 : 8
Thread No. 1 : 10
Thread No. 2 : 2
Thread No. 2 : 4
Thread No. 2 : 6
Thread No. 2 : 8
Thread No. 2 : 10
Example 3 : Synchronized Block in Static Method
Output
Thread No. 1 : 2
Thread No. 1 : 4
Thread No. 1 : 6
Thread No. 1 : 8
Thread No. 1 : 10
Thread No. 2 : 2
Thread No. 2 : 4
Thread No. 2 : 6
Thread No. 2 : 8
Thread No. 2 : 10
When we declare any method with a synchronized keyword, that method is known as a synchronized method in Java. This method is used to lock an object to a shared resource.
When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its execution.
Syntax of synchronized method
Here, lockObject is a reference to an object whose lock is associated with the monitor that the synchronized statements represent.
Example 4 : Synchronized Instance Method
Output
Thread-0 : 2
Thread-0 : 4
Thread-0 : 6
Thread-0 : 8
Thread-0 : 10
Thread-1 : 2
Thread-1 : 4
Thread-1 : 6
Thread-1 : 8
Thread-1 : 10
Same as synchronized instance method we can also synchronized static method in Java.
Example 5 : Synchronized Static Method
Output
Thread-0 : 2
Thread-0 : 4
Thread-0 : 6
Thread-0 : 8
Thread-0 : 10
Thread-1 : 2
Thread-1 : 4
Thread-1 : 6
Thread-1 : 8
Thread-1 : 10
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why Synchronization is needed in Java?
Ans. The synchronization is needed for the following reasons:
Why do we need synchronization?
Ans. For an answer, consider this example: We write a Java program that uses a pair of threads to simulate the withdrawal and deposit of financial transactions. In that program, one thread performs deposits while the other performs withdrawals. Each thread manipulates a pair of shared variables: class and instance field variables that identify the financial transaction's name and amount. To complete a valid financial transaction, each thread must finish assigning values to the name and amount variables (and then print those values to simulate saving the transaction) before the other thread begins assigning values to the name and amount variables (and also printing those values).
Wha is the difference between synchronized keyword and synchronized block?
When we use synchronized keyword with a method, it acquires a lock in the object for the whole method. It means that no other thread can use any synchronized method until the current thread, which has invoked it's synchronized method, has finished its execution.
synchronized block acquires a lock in the object only between parentheses after the synchronized keyword. This means that no other thread can acquire a lock on the locked object until the synchronized block exits. But other threads can access the rest of the code of the method.
Which is more preferred - Synchronized method or Synchronized block?
In Java, synchronized keyword causes a performance cost. A synchronized method in Java is very slow and can degrade performance. So we must use synchronization keyword in java when it is necessary else, we should use Java synchronized block that is used for synchronizing critical section only.