-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadlockExample.java
More file actions
21 lines (21 loc) · 834 Bytes
/
DeadlockExample.java
File metadata and controls
21 lines (21 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class DeadlockExample {
private static final Object Lock1 = new Object();
private static final Object Lock2 = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (Lock1) {
System.out.println("Thread1: Lock1");
try { Thread.sleep(100); } catch (Exception ignored) {}
synchronized (Lock2) { System.out.println("Thread1: Lock2"); }
}
});
Thread t2 = new Thread(() -> {
synchronized (Lock2) {
System.out.println("Thread2: Lock2");
try { Thread.sleep(100); } catch (Exception ignored) {}
synchronized (Lock1) { System.out.println("Thread2: Lock1"); }
}
});
t1.start(); t2.start();
}
}