Repository navigation
Expand file tree
/
Copy pathDeadlock.java
More file actions
67 lines (59 loc) · 2.27 KB
/
Copy pathDeadlock.java
File metadata and controls
67 lines (59 loc) · 2.27 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package Snippets.Multithreading;
/*
Blocked for Lock Acquisition state
*/
public class Deadlock {
public static void main(String[] args) {
System.out.println("Started main thread");
// Two locks
Object lock1 = "Lock1";
Object lock2 = "Lock2";
// Thread 1 tries to acquire lock1 first, then lock2
Thread thread1 = new Thread(() -> {
synchronized (lock1) {
System.out.println(Thread.currentThread().getName() + " acquired lock1");
try {
// Simulate work with lock1
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " waiting to acquire lock2");
synchronized (lock2) {
System.out.println(Thread.currentThread().getName() + " acquired lock2");
}
}
}, "Thread1");
// Thread 2 tries to acquire lock2 first, then lock1
Thread thread2 = new Thread(() -> {
synchronized (lock2) {
System.out.println(Thread.currentThread().getName() + " acquired lock2");
try {
// Simulate work with lock2
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " waiting to acquire lock1");
synchronized (lock1) {
System.out.println(Thread.currentThread().getName() + " acquired lock1");
}
}
}, "Thread2");
// Start both threads
thread1.start();
thread2.start();
// Monitor threads' states
new Thread(() -> {
while (true) {
System.out.println("Thread1 State: " + thread1.getState());
System.out.println("Thread2 State: " + thread2.getState());
try {
Thread.sleep(1000); // Check states periodically
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}