-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathDeadLockDemo.java
24 lines (23 loc) · 958 Bytes
/
DeadLockDemo.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
public class DeadLockDemo {
/* * This method request two locks, first String and then Integer */ public void method1() {
synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
}
}
}
/*
* * This method also requests same two lock but in exactly * Opposite order
* i.e. first Integer and then String. * This creates potential deadlock, if one
* thread holds String lock * and other holds Integer lock and they wait for
* each other, forever.
*/ public void method2() {
synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}
}