forked from theonlyanson/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadlock.java
More file actions
71 lines (50 loc) · 1.63 KB
/
Deadlock.java
File metadata and controls
71 lines (50 loc) · 1.63 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
67
68
69
70
71
import static deadlock.Deadlock.lock1;
import static deadlock.Deadlock.lock2;
/**
*
* @author Lakshitha Samod
*/
public class Deadlock {
public static String lock1 = "Sam";
public static String lock2 = "Kamal";
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.start();
Thread2 t2 = new Thread2();
t2.start();
}
}
class Thread1 extends Thread {
@Override
public void run() {
System.out.println("start executing thread 1");
synchronized (lock1) {
System.out.println("Thread 1 holding lock 1...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
System.out.println("thread 1 is waiting for the lock 2");
synchronized (lock2) {
System.out.println("Thread 1 holding lock 1 and lock 2...");
}
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
System.out.println("start executing thread 2");
synchronized (lock2) {
System.out.println("Thread 2 holding lock 2...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
System.out.println("thread 2 is waiting for the lock 1");
synchronized (lock1) {
System.out.println("Thread 2 holding lock 1 and lock 2...");
}
}
}
}