In a multi-threaded Java application, a deadlock occurs when two threads wait forever attempting to acquire locks that are held by each other. Here’s a simple example to simulate a deadlock:
public class Deadlock {
private Object lock1;
private Object lock2;
public Deadlock(Object lock1, Object lock2) {
this.lock1 = lock1;
this.lock2 = lock2;
}
public void methodA() {
System.out.println("trying to acquire lock1 from - " + Thread.currentThread().getName());
synchronized (lock1) {
someLongRunningTask();
methodB();
}
}
public void methodB() {
System.out.println("trying to acquire lock2 from - " + Thread.currentThread().getName());
synchronized (lock2) {
someLongRunningTask();
methodA();
}
}
private void someLongRunningTask() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[]args) {
Object lock1 = new Object();
Object lock2 = new Object();
new Thread(() -> new Deadlock(lock1, lock2).methodA()).start();
new Thread(() -> new Deadlock(lock1, lock2).methodB()).start();
}
}
First thread calls
methodA and acquires
lock1. Second thread calls
methodB and acquires
lock2. Then the first thread calls
methodB while at the same time the second thread calls
methodA. Both are trying to acquire a lock that is already held by another thread, so neither can proceed further.
[Read more]