Home >Java >javaTutorial >How to prevent and deal with deadlocks in Java concurrent programming?
Deadlock is a common problem in concurrent programming and can be prevented or handled by taking measures: Preventing deadlock: - Acquire locks in order - Avoid circular waiting - Use timeout mechanism - Use non-blocking data structures to handle deadlock: - Dead Lock detection-deadlock recovery-retry operation
Deadlock prevention and processing in Java concurrent programming
Deadlock is A common problem you may encounter in concurrent programming is that multiple threads wait for each other to release resources, causing the system to deadlock. In Java, deadlocks can be prevented or handled by taking appropriate measures.
Prevent deadlock
Handling Deadlocks
If precautions do not prevent deadlocks, you can handle deadlocks by:
Practical case
Consider the following Java code snippet:
public class DeadlockExample { private final Object lock1 = new Object(); private final Object lock2 = new Object(); public void method1() { synchronized (lock1) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock1"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock2) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock2"); } } } public void method2() { synchronized (lock2) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock2"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock1) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock1"); } } } public static void main(String[] args) { DeadlockExample deadlockExample = new DeadlockExample(); Thread thread1 = new Thread(deadlockExample::method1); Thread thread2 = new Thread(deadlockExample::method2); thread1.start(); thread2.start(); } }
In this example, the two threads (thread1 and thread2) respectively Use lock1 and lock2 for synchronization. Since both threads acquire locks in reverse order, they wait for each other to release the lock, resulting in a deadlock.
To prevent deadlock, we can modify the code to acquire locks in order:
public class DeadlockExample { private final Object lock1 = new Object(); private final Object lock2 = new Object(); public void method1() { synchronized (lock1) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock1"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock2) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock2"); } } } public void method2() { synchronized (lock2) { System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock2"); synchronized (lock1) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + Thread.currentThread().getName() + " acquired lock1"); } } } public static void main(String[] args) { DeadlockExample deadlockExample = new DeadlockExample(); Thread thread1 = new Thread(deadlockExample::method1); Thread thread2 = new Thread(deadlockExample::method2); thread1.start(); thread2.start(); } }
By modifying the code, we ensure that thread1 and thread2 always acquire in the same order (lock1 then lock2) lock to prevent deadlock.
The above is the detailed content of How to prevent and deal with deadlocks in Java concurrent programming?. For more information, please follow other related articles on the PHP Chinese website!