-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadlock.java
More file actions
39 lines (36 loc) · 952 Bytes
/
Copy pathDeadlock.java
File metadata and controls
39 lines (36 loc) · 952 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
36
37
38
39
import java.util.*;
public class Deadlock{
public static void main(String[] args){
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}
class Test implements Runnable{
private boolean flag;
public Test(boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
synchronized(Mylock.obj1){
System.out.println("if obj1");
synchronized(Mylock.obj2){
System.out.println("else obj1");
}
}
}else{
synchronized(Mylock.obj2){
System.out.println("if obj2");
synchronized(Mylock.obj1){
System.out.println("else obj2");
}
}
}
}
}
class Mylock{
static Object obj1 = new Object();
static Object obj2 = new Object();
}