Skip to content

Commit c6cc50c

Browse files
committed
🔖 死锁示例
1 parent d959561 commit c6cc50c

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.github.dunwu.javase.concurrent.lock;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* 死锁示例
10+
* @see DeadLockFixDemo
11+
*/
12+
@SuppressWarnings("all")
13+
public class DeadLockDemo {
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
List<Integer> list1 = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10));
17+
List<Integer> list2 = new ArrayList<>(Arrays.asList(1, 3, 7, 9, 11));
18+
19+
Thread thread1 = new Thread(() -> {
20+
moveListItem(list1, list2, 2);
21+
});
22+
Thread thread2 = new Thread(() -> {
23+
moveListItem(list2, list1, 9);
24+
});
25+
26+
thread1.start();
27+
thread2.start();
28+
thread1.join();
29+
thread2.join();
30+
System.out.println(list1);
31+
System.out.println(list2);
32+
}
33+
34+
private static void moveListItem(List<Integer> from, List<Integer> to, Integer item) {
35+
log("attempting lock for list", from);
36+
synchronized (from) {
37+
log("lock acquired for list", from);
38+
try {
39+
TimeUnit.SECONDS.sleep(1);
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
log("attempting lock for list ", to);
44+
synchronized (to) {
45+
log("lock acquired for list", to);
46+
if (from.remove(item)) {
47+
to.add(item);
48+
}
49+
log("moved item to list ", to);
50+
}
51+
}
52+
}
53+
54+
private static void log(String msg, Object target) {
55+
System.out.println(Thread.currentThread().getName() + ": " + msg + " " + System.identityHashCode(target));
56+
}
57+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.dunwu.javase.concurrent.lock;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* 解决死锁示例
10+
* @see DeadLockDemo
11+
*/
12+
@SuppressWarnings("all")
13+
public class DeadLockFixDemo {
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
List<Integer> list1 = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10));
17+
List<Integer> list2 = new ArrayList<>(Arrays.asList(1, 3, 7, 9, 11));
18+
19+
Thread thread1 = new Thread(() -> {
20+
moveListItem(list1, list2, 2);
21+
});
22+
Thread thread2 = new Thread(() -> {
23+
moveListItem(list2, list1, 9);
24+
});
25+
26+
thread1.start();
27+
thread2.start();
28+
thread1.join();
29+
thread2.join();
30+
System.out.println(list1);
31+
System.out.println(list2);
32+
}
33+
34+
private static void moveListItem(List<Integer> from, List<Integer> to, Integer item) {
35+
log("attempting lock for list", from);
36+
boolean removedSuccessful = false;
37+
38+
synchronized (from) {
39+
log("lock acquired for list", from);
40+
try {
41+
TimeUnit.SECONDS.sleep(1);
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
removedSuccessful = from.remove(item);
46+
}
47+
if (removedSuccessful) {
48+
log("attempting lock for list ", to);
49+
synchronized (to) {
50+
log("lock acquired for list", to);
51+
52+
to.add(item);
53+
log("moved item to list ", to);
54+
}
55+
}
56+
}
57+
58+
private static void log(String msg, Object target) {
59+
System.out.println(Thread.currentThread().getName() + ": " + msg + " " + System.identityHashCode(target));
60+
}
61+
}

0 commit comments

Comments
 (0)