Thread Synchronization in Java

📘 Java 👁 48 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

Thread Synchronization is a mechanism that ensures multiple threads do not access shared resources simultaneously, preventing data inconsistency and race conditions in Java programs.


1. Why Synchronization is Needed

When multiple threads access shared data concurrently:

  • The shared data can be corrupted.

  • Operations like incrementing or updating a variable may interleave, producing incorrect results.

  • Synchronization ensures thread-safe access to critical resources.

Example of a problem without synchronization:

class Counter { int count = 0; void increment() { count++; // Race condition may occur } } public class Test { public static void main(String[] args) { Counter c = new Counter(); Thread t1 = new Thread(() -> { for(int i=0;i<1000;i++) c.increment(); }); Thread t2 = new Thread(() -> { for(int i=0;i<1000;i++) c.increment(); }); t1.start(); t2.start(); } }

The final value of count may not be 2000 due to race conditions.


2. The synchronized Keyword

The synchronized keyword ensures that only one thread can execute a method or block at a time for a particular object.

(a) Synchronized Method

class Counter { int count = 0; synchronized void increment() { count++; } }
  • Only one thread can execute increment() at a time.

  • Ensures atomicity and consistency.

(b) Synchronized Block

class Counter { int count = 0; void increment() { synchronized(this) { count++; } } }
  • Only the critical section is synchronized.

  • Improves performance by reducing unnecessary locking.


3. Thread Communication

Synchronization also enables thread communication using:

  • wait(): Causes a thread to release the lock and wait.

  • notify(): Wakes one waiting thread.

  • notifyAll(): Wakes all waiting threads.

Example:

class Data { int value; boolean available = false; synchronized void produce(int val) throws InterruptedException { while(available) wait(); value = val; available = true; notify(); } synchronized int consume() throws InterruptedException { while(!available) wait(); available = false; notify(); return value; } }

4. Advantages of Thread Synchronization

  • Prevents data inconsistency in shared resources.

  • Avoids race conditions.

  • Maintains thread safety in concurrent applications.

  • Enables cooperative multitasking using wait and notify.


5. Key Points

  • Synchronization can be applied to methods or blocks.

  • Only one thread can hold the lock on an object at a time.

  • Excessive synchronization may reduce performance (bottleneck).

  • Java provides higher-level concurrency utilities in java.util.concurrent package for advanced thread control.


6. Conclusion

Thread synchronization in Java ensures that shared resources are accessed safely by multiple threads. Using synchronized, wait, and notify, programmers can prevent race conditions, maintain data integrity, and design reliable multithreaded applications.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes