-
Notifications
You must be signed in to change notification settings - Fork 6
/
Thread_Deadlock.java
79 lines (71 loc) · 1.32 KB
/
Thread_Deadlock.java
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
72
73
74
75
76
77
78
79
class Util
{
static void sleep(long millis)
{
try
{
Thread.sleep(millis);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
class Shared
{
synchronized void test1(Shared s2)
{
System.out.println("test1 begins");
Util.sleep(1000);
s2.test2();
System.out.println("test1 ends");
}
synchronized void test2()
{
System.out.println("test2 begins");
Util.sleep(1000);
System.out.println("test2 ends");
}
}
class Thread1 extends Thread
{
private final Shared s1;
private final Shared s2;
public Thread1(Shared s1,Shared s2)
{
this.s1=s1;
this.s2=s2;
}
public void run()
{
s1.test1(s2);
}
}
class Thread2 extends Thread
{
private final Shared s1;
private final Shared s2;
public Thread2(Shared s1,Shared s2)
{
this.s1=s1;
this.s2=s2;
}
public void run()
{
s2.test1(s1);
}
}
public class Thread_Deadlock
{
public static void main(String[]args)
{
Shared s1=new Shared();
Shared s2=new Shared();
Thread1 t1=new Thread1(s1,s2);
t1.start();
Thread2 t2=new Thread2(s1,s2);
t2.start();
Util.sleep(2000);
}
}