-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainConcurrency.java
More file actions
30 lines (27 loc) · 1.12 KB
/
MainConcurrency.java
File metadata and controls
30 lines (27 loc) · 1.12 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
package ru.javaops.webapp;
public class MainConcurrency {
static final String LOCK1 = "LOCK1";
static final String LOCK2 = "LOCK2";
public static void main(String[] args) {
deadlock(LOCK1, LOCK2);
deadlock(LOCK2, LOCK1);
}
public static void deadlock(Object obj1, Object obj2) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + " started");
System.out.println(Thread.currentThread().getName() + " trying to synchronize by " + obj1);
synchronized (obj1) {
System.out.println(Thread.currentThread().getName() + " synchronized by " + obj1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName() + " trying to synchronize by " + obj2);
synchronized (obj2) {
System.out.println(Thread.currentThread().getName() + " synchronized by " + obj2);
}
}
}).start();
}
}