-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadlock.java
More file actions
36 lines (30 loc) · 881 Bytes
/
Copy pathDeadlock.java
File metadata and controls
36 lines (30 loc) · 881 Bytes
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
public class Deadlock {
private Object process = new Object();
private boolean stopped = true;
public synchronized void run() {
synchronized (process) {
isStopped();
}
}
private synchronized boolean isStopped() {
return stopped;
}
public synchronized void stopProcess() {
synchronized (process) {
System.out.println("Stopping...");
}
}
public static void main(String args[]) throws InterruptedException {
Deadlock deadlock = new Deadlock();
Thread threadA = new Thread(() -> {
deadlock.run();
System.out.println("Running...");
});
Thread threadB = new Thread(() -> {
deadlock.stopProcess();
System.out.println("Stopped...");
});
threadA.start();
threadB.start();
}
}