forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadLockFixedDemo.java
More file actions
27 lines (24 loc) · 822 Bytes
/
ThreadLockFixedDemo.java
File metadata and controls
27 lines (24 loc) · 822 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
package deadlock;
public class ThreadLockFixedDemo {
/* Both method are now requesting lock in same order,
* first Integer and then String.
* This will solve the problem,
* as long as both method are requesting lock in consistent order.
* */
public void method1() {
synchronized (Integer.class) {
System.out.println("Acquired lock on Integer.class object");
}
synchronized (String.class) {
System.out.println("Acquired lock on String.class object");
}
}
public void method2() {
synchronized (Integer.class) {
System.out.println("Acquired lock on Integer.class object");
}
synchronized (String.class) {
System.out.println("Acquired lock on String.class object");
}
}
}