Skip to content

Commit 5038892

Browse files
author
Tanechka
committed
Lesson12 HW11
1 parent 6a626f6 commit 5038892

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/ru/javawebinar/basejava/MainConcurrency.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ private void inc() {
5858
}
5959
});
6060
System.out.println(mainConcurrency.counter);
61+
62+
final String lock1 = "lock1";
63+
final String lock2 = "lock2";
64+
deadLock(lock1, lock2);
65+
deadLock(lock2, lock1);
66+
67+
}
68+
69+
private static void deadLock(Object lock1, Object lock2) {
70+
new Thread(() -> {
71+
System.out.println("Waiting " + lock1);
72+
synchronized (lock1) {
73+
System.out.println("Holding " + lock1);
74+
try {
75+
Thread.sleep(50);
76+
} catch (InterruptedException e) {
77+
e.printStackTrace();
78+
}
79+
System.out.println("Waiting " + lock2);
80+
synchronized (lock2) {
81+
System.out.println("Holding " + lock2);
82+
}
83+
}
84+
}).start();
6185
}
6286

6387
private synchronized void inc() {

0 commit comments

Comments
 (0)