-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeadLock.java
More file actions
64 lines (58 loc) · 2.07 KB
/
Copy pathDeadLock.java
File metadata and controls
64 lines (58 loc) · 2.07 KB
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
// Copyright: Ganesh Samarthyam, 2016; www.codeops.tech
// Balls class has a globally accessible data member to hold the number of balls thrown
class Balls {
public static long balls = 0;
}
// Runs class has a globally accessible data member to hold the number of runs scored
class Runs {
public static long runs = 0;
}
// Counter class has two methods – IncrementBallAfterRun and IncrementRunAfterBall.
// For demonstrating deadlock, we call these two methods in the run method, so that
// locking can be requested in opposite order in these two methods
class Counter implements Runnable {
// this method increments runs variable first and then increments the balls variable
// since these variables are accessible from other threads,
// we need to acquire a lock before processing them
public void IncrementBallAfterRun() {
// since we’re updating runs variable first, first lock the Runs.class
synchronized(Runs.class) {
// lock on Balls.class before updating balls variable
synchronized(Balls.class) {
Runs.runs++;
Balls.balls++;
}
}
}
public void IncrementRunAfterBall() {
// since we’re updating balls variable first; so first lock Balls.class
synchronized(Balls.class) {
// acquire lock on Runs.class before updating runs variable
synchronized(Runs.class) {
Balls.balls++;
Runs.runs++;
}
}
}
public void run() {
// call these two methods which acquire locks in different order
// depending on thread scheduling and the order of lock acquision,
// a deadlock may or may not arise
IncrementBallAfterRun();
IncrementRunAfterBall();
}
}
public class DeadLock {
public static void main(String args[]) throws InterruptedException {
Counter c = new Counter();
// create two threads and start them at the same time
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
System.out.println("Waiting for threads to complete execution…");
t1.join();
t2.join();
System.out.println("Done.");
}
}