Synchronized Keyword in Java

In Java, concurrent programming is a powerful technique that allows multiple threads to execute tasks simultaneously, improving application performance. However, it also introduces challenges related to thread synchronization and potential data inconsistency issues. To address these problems, Java provides the synchronized keyword, which plays a crucial role in managing shared resources and ensuring thread safety. In this blog, we will dive into the concept of the synchronized keyword and explore how it helps developers tackle concurrency-related problems in Java.

Problem with multiple threads

See the above code, I have created a thread class that increments the integer "number" by 1 thousand times. Then I created two threads and started the thread.

As per the code, each thread will increment the value of "number" 1000 times and as we have two threads, increment will happen two thousands time. So the output should be 2000.

But you can see in the above code, sometimes we get output as 2000 and sometime we get less than 2000.

why? : in the thread class two operations are happening in for-loop, one is reading the value of "number" and another is doing increment.

let's say t1 reads the value as 12, then t2 comes in and reads the value as 12 and does increment and assigns it the value 13, now as t1 already read the value as 12 so t1 also increments it and makes it 13.

Because of this, we are seeing data loss.

Solution using synchronized keyword

The Synchronized Keyword

The synchronized keyword is used to lock a particular block of code or method for a thread, that means if a thread is executing a synchronized block/method then no other thread can execute that until the first thread completes that block.

Usage of Synchronized Keyword

  1. Synchronized Methods:

    See the below code, to solve the above problem I have extracted the process of increment to a synchronized method.

  2. Synchronized Statements/block:

    See the below code, to solve the above problem I have kept the process of increment inside a synchronized block.

Benefits of Synchronized Keyword

  1. Thread Safety: By using the synchronized keyword, developers can ensure that critical sections of code are executed by only one thread at a time, preventing race conditions and data corruption.

  2. Memory Visibility: Synchronization not only provides mutual exclusion but also ensures memory visibility. Changes made by one thread inside a synchronized block are visible to other threads after they acquire the lock.

  3. Simple to Use: The synchronized keyword is relatively easy to use and provides an effective way to control concurrency in Java programs.

Drawbacks of Synchronized Keyword

  1. Overhead: The use of synchronization can introduce some performance overhead due to the need to acquire and release locks.

  2. Deadlocks: Improper usage of synchronized blocks can lead to deadlocks, where threads are stuck waiting for each other indefinitely.

Conclusion

The synchronized keyword is a vital tool in Java for managing concurrency and ensuring thread safety. By synchronizing critical sections of code, developers can prevent data inconsistencies and race conditions, allowing multiple threads to coexist harmoniously. However, it is essential to use synchronization judiciously and avoid potential pitfalls like deadlocks. Understanding the proper use of synchronization is essential for building robust and efficient concurrent applications in Java.

Did you find this article valuable?

Support Java Blogs By Hemant by becoming a sponsor. Any amount is appreciated!