forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplicitCriticalSection.java
More file actions
79 lines (76 loc) · 2.34 KB
/
ExplicitCriticalSection.java
File metadata and controls
79 lines (76 loc) · 2.34 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// threads/ExplicitCriticalSection.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ThrowsException} on a multiprocessor machine
// Using explicit Lock objects to create critical sections
// {java threads.ExplicitCriticalSection}
package threads;
import java.util.concurrent.locks.*;
// Synchronize the entire method:
class ExplicitPairManager1 extends PairManager {
private Lock lock = new ReentrantLock();
@Override
public void increment() {
lock.lock();
try {
p.incrementX();
p.incrementY();
store(getPair());
} finally {
lock.unlock();
}
}
}
// Use a critical section:
class ExplicitPairManager2 extends PairManager {
private Lock lock = new ReentrantLock();
@Override
public void increment() {
Pair temp;
lock.lock();
try {
p.incrementX();
p.incrementY();
temp = getPair();
} finally {
lock.unlock();
}
store(temp);
}
}
public class ExplicitCriticalSection {
public static void
main(String[] args) throws Exception {
PairManager
pman1 = new ExplicitPairManager1(),
pman2 = new ExplicitPairManager2();
CriticalSection.testApproaches(pman1, pman2);
}
}
/* Output:
pm1: Pair: x: 10, y: 10 checkCounter = 1713716
pm2: Pair: x: 10, y: 10 checkCounter = 1717747
___[ Error Output ]___
Exception in thread "pool-1-thread-4" Exception in thread
"pool-1-thread-3" threads.Pair$PairValuesNotEqualException:
Pair values not equal: x: 2, y: 1
at threads.Pair.checkState(CriticalSection.java:37)
at
threads.PairChecker.run(CriticalSection.java:111)
at java.util.concurrent.ThreadPoolExecutor.runWorke
r(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.r
un(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
threads.Pair$PairValuesNotEqualException: Pair values not
equal: x: 2, y: 1
at threads.Pair.checkState(CriticalSection.java:37)
at
threads.PairChecker.run(CriticalSection.java:111)
at java.util.concurrent.ThreadPoolExecutor.runWorke
r(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.r
un(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
*/