Multithreading is a powerful concept in Java, allowing programs to execute multiple threads concurrently. However, this ability places the onus of managing synchronization, ensuring that threads do not interfere with each other and produce unexpected results, on the developer. Thread synchronization errors can be elusive and challenging to detect, making them a common source of bugs in multithreaded Java applications. This tutorial describes the various types of thread synchronization errors and offer suggestions for fixing them.
Jump to:
Race Conditions
A race condition occurs when the behavior of a program depends on the relative timing of events, such as the order in which threads are scheduled to run. This can lead to unpredictable results and data corruption. Consider the following example:
public class RaceConditionExample { Â Â Â Â private static int counter = 0; Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Runnable incrementTask = () -> { Â Â Â Â Â Â Â Â Â Â Â Â for (int i = 0; i < 10000; i++) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â counter++; Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Thread thread1 = new Thread(incrementTask); Â Â Â Â Â Â Â Â Thread thread2 = new Thread(incrementTask); Â Â Â Â Â Â Â Â thread1.start(); Â Â Â Â Â Â Â Â thread2.start(); Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â thread1.join(); Â Â Â Â Â Â Â Â Â Â Â Â thread2.join(); Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â System.out.println("Counter: " + counter); Â Â Â Â } }
In this example, two threads are incrementing a shared counter variable. Due to the lack of synchronization, a race condition occurs, and the final value of the counter is unpredictable. To fix this, we can use the synchronized keyword:
public class FixedRaceConditionExample { Â Â Â Â private static int counter = 0; Â Â Â Â public static synchronized void increment() { Â Â Â Â Â Â Â Â for (int i = 0; i < 10000; i++) { Â Â Â Â Â Â Â Â Â Â Â Â counter++; Â Â Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Thread thread1 = new Thread(FixedRaceConditionExample::increment); Â Â Â Â Â Â Â Â Thread thread2 = new Thread(FixedRaceConditionExample::increment); Â Â Â Â Â Â Â Â thread1.start(); Â Â Â Â Â Â Â Â thread2.start(); Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â thread1.join(); Â Â Â Â Â Â Â Â Â Â Â Â thread2.join(); Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â System.out.println("Counter: " + counter); Â Â Â Â } }
Using the synchronized keyword on the increment method ensures that only one thread can execute it at a time, thus preventing the race condition.
Detecting race conditions requires careful analysis of your code and understanding the interactions between threads. Always use synchronization mechanisms, such as synchronized methods or blocks, to protect shared resources and avoid race conditions.
Deadlocks
Deadlocks occur when two or more threads are blocked forever, each waiting for the other to release a lock. This situation can bring your application to a standstill. Let’s consider a classic example of a deadlock:
public class DeadlockExample { Â Â Â Â private static final Object lock1 = new Object(); Â Â Â Â private static final Object lock2 = new Object(); Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Thread thread1 = new Thread(() -> { Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock1) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 1: Holding lock 1"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Thread.sleep(100); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 1: Waiting for lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock2) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 1: Holding lock 1 and lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â Thread thread2 = new Thread(() -> { Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock2) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 2: Holding lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Thread.sleep(100); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 2: Waiting for lock 1"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock1) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 2: Holding lock 2 and lock 1"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â thread1.start(); Â Â Â Â Â Â Â Â thread2.start(); Â Â Â Â } }
In this example, Thread 1 holds lock1 and waits for lock2, while Thread 2 holds lock2 and waits for lock1. This results in a deadlock, as neither thread can proceed.
To avoid deadlocks, ensure that threads always acquire locks in the same order. If multiple locks are needed, use a consistent order to acquire them. Here’s a modified version of the previous example that avoids the deadlock:
public class FixedDeadlockExample { Â Â Â Â private static final Object lock1 = new Object(); Â Â Â Â private static final Object lock2 = new Object(); Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Thread thread1 = new Thread(() -> { Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock1) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 1: Holding lock 1"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Thread.sleep(100); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 1: Waiting for lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock2) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 1: Holding lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â Thread thread2 = new Thread(() -> { Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock1) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 2: Holding lock 1"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Thread.sleep(100); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 2: Waiting for lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock2) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Thread 2: Holding lock 2"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â thread1.start(); Â Â Â Â Â Â Â Â thread2.start(); Â Â Â Â } }
In this fixed version, both threads acquire locks in the same order: first lock1, then lock2. This eliminates the possibility of a deadlock.
Preventing deadlocks involves careful design of your locking strategy. Always acquire locks in a consistent order to avoid circular dependencies between threads. Use tools like thread dumps and profilers to identify and resolve deadlock issues in your Java programs. Also, consider reading our tutorial on How to Prevent Thread Deadlocks in Java for even more strategies.
Starvation
Starvation occurs when a thread is unable to gain regular access to shared resources and is unable to make progress. This can happen when a thread with a lower priority is constantly preempted by threads with higher priorities. Consider the following code example:
public class StarvationExample { Â Â Â Â private static final Object lock = new Object(); Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Thread highPriorityThread = new Thread(() -> { Â Â Â Â Â Â Â Â Â Â Â Â while (true) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("High Priority Thread is working"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â Thread lowPriorityThread = new Thread(() -> { Â Â Â Â Â Â Â Â Â Â Â Â while (true) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â synchronized (lock) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println("Low Priority Thread is working"); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â highPriorityThread.setPriority(Thread.MAX_PRIORITY); Â Â Â Â Â Â Â Â lowPriorityThread.setPriority(Thread.MIN_PRIORITY); Â Â Â Â Â Â Â Â highPriorityThread.start(); Â Â Â Â Â Â Â Â lowPriorityThread.start(); Â Â Â Â } }
In this example, we have a high-priority thread and a low-priority thread both contending for a lock. The high-priority thread dominates, and the low-priority thread experiences starvation.
To mitigate starvation, you can use fair locks or adjust thread priorities. Here’s an updated version using a ReentrantLock with the fairness flag enabled:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class FixedStarvationExample {     // The true boolean value enables fairness     private static final Lock lock = new ReentrantLock(true);     public static void main(String[] args) {         Thread highPriorityThread = new Thread(() -> {             while (true) {                 lock.lock();                 try {                     System.out.println("High Priority Thread is working");                 } finally {                     lock.unlock();                 }             }         });         Thread lowPriorityThread = new Thread(() -> {             while (true) {                 lock.lock();                 try {                     System.out.println("Low Priority Thread is working");                 } finally {                     lock.unlock();                 }             }         });         highPriorityThread.setPriority(Thread.MAX_PRIORITY);         lowPriorityThread.setPriority(Thread.MIN_PRIORITY);         highPriorityThread.start();         lowPriorityThread.start();     } }
The ReentrantLock with fairness ensures that the longest-waiting thread gets the lock, reducing the likelihood of starvation.
Mitigating starvation involves carefully considering thread priorities, using fair locks, and ensuring that all threads have equitable access to shared resources. Regularly review and adjust your thread priorities based on the requirements of your application.
Check out our tutorial on the Best Threading Practices for Java Applications.
Data Inconsistency
Data inconsistency occurs when multiple threads access shared data without proper synchronization, leading to unexpected and incorrect results. Consider the following example:
public class DataInconsistencyExample { Â Â Â Â private static int sharedValue = 0; Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Runnable incrementTask = () -> { Â Â Â Â Â Â Â Â Â Â Â Â for (int i = 0; i < 1000; i++) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sharedValue++; Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Thread thread1 = new Thread(incrementTask); Â Â Â Â Â Â Â Â Thread thread2 = new Thread(incrementTask); Â Â Â Â Â Â Â Â thread1.start(); Â Â Â Â Â Â Â Â thread2.start(); Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â thread1.join(); Â Â Â Â Â Â Â Â Â Â Â Â thread2.join(); Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â System.out.println("Shared Value: " + sharedValue); Â Â Â Â } }
In this example, two threads are incrementing a shared value without synchronization. As a result, the final value of the shared value is unpredictable and inconsistent.
To fix data inconsistency issues, you can use the synchronized keyword or other synchronization mechanisms:
public class FixedDataInconsistencyExample { Â Â Â Â private static int sharedValue = 0; Â Â Â Â public static synchronized void increment() { Â Â Â Â Â Â Â Â for (int i = 0; i < 1000; i++) { Â Â Â Â Â Â Â Â Â Â Â Â sharedValue++; Â Â Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â Thread thread1 = new Thread(FixedDataInconsistencyExample::increment); Â Â Â Â Â Â Â Â Thread thread2 = new Thread(FixedDataInconsistencyExample::increment); Â Â Â Â Â Â Â Â thread1.start(); Â Â Â Â Â Â Â Â thread2.start(); Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â thread1.join(); Â Â Â Â Â Â Â Â Â Â Â Â thread2.join(); Â Â Â Â Â Â Â Â } catch (InterruptedException e) { Â Â Â Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â System.out.println("Shared Value: " + sharedValue); Â Â Â Â } }
Using the synchronized keyword on the increment method ensures that only one thread can execute it at a time, preventing data inconsistency.
To avoid data inconsistency, always synchronize access to shared data. Use the synchronized keyword or other synchronization mechanisms to protect critical sections of code. Regularly review your code for potential data inconsistency issues, especially in multithreaded environments.
Final Thoughts on Detecting and Fixing Thread Synchronization Errors in Java
In this Java tutorial, we explored practical examples of each type of thread synchronization error and provided solutions to fix them. Thread synchronization errors, such as race conditions, deadlocks, starvation, and data inconsistency, can introduce subtle and hard-to-find bugs. However, by incorporating the strategies presented here into your Java code, you can enhance the stability and performance of your multithreaded applications.