-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadLock.java
More file actions
31 lines (25 loc) · 1 KB
/
DeadLock.java
File metadata and controls
31 lines (25 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class DeadLock {
private final static Object fistLock = new Object();
public final static Object secondLock = new Object();
public static void main(String[] args) {
new Thread(() -> testForDeadLock(fistLock, secondLock)).start();
new Thread(() -> testForDeadLock(secondLock, fistLock)).start();
}
public static void testForDeadLock(Object fistLock, Object secondLock) {
synchronized (fistLock) {
System.out.println(getCurrentThread() + ": Holding lock-" + fistLock);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getCurrentThread() + ": Waiting for lock-" + secondLock);
synchronized (secondLock) {
System.out.println(getCurrentThread() + ": Holding both.");
}
}
}
private static String getCurrentThread() {
return Thread.currentThread().toString();
}
}