-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeadLock.java
53 lines (52 loc) · 1.24 KB
/
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
class BookTicket extends Thread{
Object train,comp;
BookTicket(Object train,Object comp){
this.train=train;
this.comp=comp;
}
public void run(){
synchronized(train){
System.out.println("BookTicket lock on train");
try{
Thread.sleep(150);
}
catch(InterruptedException e){}
System.out.println("BookTicket now waiting for lock on Compartment");
synchronized(comp){
System.out.println("BookTicket lock on Compartment");
}
}
}
}
class CancelTicket extends Thread{
Object train,comp;
CancelTicket(Object train,Object comp){
this.train=train;
this.comp=comp;
}
public void run(){
synchronized(comp){
System.out.println("CancelTicket locked on Compartment");
try{
Thread.sleep(200);
}
catch(InterruptedException e){}
System.out.println("CancelTicket ticket waiting......");
synchronized(train){
System.out.println("CancelTicket lock on train");
}
}
}
}
class DeadLock{
public static void main(String args[])throws Exception{
Object train=new Object();
Object compartment=new Object();
BookTicket ob1=new BookTicket(train,compartment);
CancelTicket ob2=new CancelTicket(train,compartment);
Thread t1=new Thread(ob1);
Thread t2=new Thread(ob2);
t1.start();
t2.start();
}
}