forked from oldratlee/fucking-java-concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymmetricLockDeadlockDemo.java
44 lines (40 loc) · 1.29 KB
/
SymmetricLockDeadlockDemo.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.oldratlee.fucking.concurrency;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
public class SymmetricLockDeadlockDemo {
static final Object lock1 = new Object();
static final Object lock2 = new Object();
public static void main(String[] args) throws Exception {
Thread thread1 = new Thread(new ConcurrencyCheckTask1());
thread1.start();
Thread thread2 = new Thread(new ConcurrencyCheckTask2());
thread2.start();
}
private static class ConcurrencyCheckTask1 implements Runnable {
@Override
public void run() {
System.out.println("ConcurrencyCheckTask1 started!");
while (true) {
synchronized (lock1) {
synchronized (lock2) {
System.out.println("Hello1");
}
}
}
}
}
private static class ConcurrencyCheckTask2 implements Runnable {
@Override
public void run() {
System.out.println("ConcurrencyCheckTask2 started!");
while (true) {
synchronized (lock2) {
synchronized (lock1) {
System.out.println("Hello2");
}
}
}
}
}
}