-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStampedLockDemo.java
More file actions
161 lines (147 loc) · 5.58 KB
/
Copy pathStampedLockDemo.java
File metadata and controls
161 lines (147 loc) · 5.58 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Phase7_Concurrency.Multithreading;
import java.util.concurrent.locks.StampedLock;
/**
* java.util.concurrent.locks.StampedLock (Java 8+)
* -------------------------------------------------
* Three modes:
* <p>
*
* WRITE LOCK - exclusive (like a write lock).
* READ LOCK - shared (like a read lock).
* OPTIMISTIC READ- NO lock acquired. Returns a "stamp"; the reader
* validates the stamp after reading. If a writer
* completed in the meantime, the validation fails
* and you fall back to a real read lock.
* <p>
*
* Optimistic reads are CHEAP — basically a volatile read — but you must
* COPY the fields you read to LOCAL VARIABLES before validating. If you
* dereference a stale reference during the optimistic window you can
* get inconsistent or even unsafe values.
* <p>
*
* Key API
* -------
* long writeLock() -> stamp
* unlockWrite(stamp)
* long readLock() -> stamp
* unlockRead(stamp)
* long tryOptimisticRead() -> stamp (0 means a writer is active)
* boolean validate(stamp) -> true if no writer since the stamp
* long tryConvertToWriteLock(stamp)-> upgrade (returns 0 on failure)
* long tryConvertToReadLock(stamp) -> downgrade
* boolean isWriteLocked()
* <p>
*
* Important differences from ReadWriteLock
* ----------------------------------------
* - NOT REENTRANT. The same thread acquiring twice deadlocks.
* - No Conditions.
* - Often faster under read-heavy contention.
* - Slightly more code to use correctly.
* <p>
*
* Pattern: read-mostly Point class
* --------------------------------
* class Point {
* private double x, y;
* private final StampedLock sl = new StampedLock();
* <p>
*
* double distanceFromOrigin() {
* long stamp = sl.tryOptimisticRead();
* double localX = x, localY = y; // COPY locally
* if (!sl.validate(stamp)) { // fall back to real read
* stamp = sl.readLock();
* try {
* localX = x; localY = y;
* } finally { sl.unlockRead(stamp); }
* }
* return Math.hypot(localX, localY);
* }
* <p>
*
* void move(double dx, double dy) {
* long stamp = sl.writeLock();
* try { x += dx; y += dy; }
* finally { sl.unlockWrite(stamp); }
* }
* }
*/
public class StampedLockDemo {
static class Point {
private double x, y;
private final StampedLock sl = new StampedLock();
double distanceFromOrigin() {
long stamp = sl.tryOptimisticRead();
double localX = x, localY = y;
if (!sl.validate(stamp)) {
stamp = sl.readLock();
try {
localX = x; localY = y;
} finally { sl.unlockRead(stamp); }
}
return Math.hypot(localX, localY);
}
void move(double dx, double dy) {
long stamp = sl.writeLock();
try { x += dx; y += dy; }
finally { sl.unlockWrite(stamp); }
}
/** Conditional move: only if currently at origin. Demonstrates conversion. */
void moveIfAtOrigin(double newX, double newY) {
long stamp = sl.readLock();
try {
while (x == 0.0 && y == 0.0) {
long ws = sl.tryConvertToWriteLock(stamp);
if (ws != 0L) { stamp = ws; x = newX; y = newY; break; }
else { sl.unlockRead(stamp); stamp = sl.writeLock(); }
}
} finally {
sl.unlock(stamp);
}
}
}
public static void main(String[] args) throws InterruptedException {
section("1) Read-mostly Point — optimistic reads usually succeed");
Point p = new Point();
p.move(3, 4);
System.out.println("distance = " + p.distanceFromOrigin());
section("2) Stress test — readers see consistent (x, y)");
Point pt = new Point();
Thread writer = new Thread(() -> {
for (int i = 0; i < 1_000; i++) {
pt.move(0.001, -0.001);
try { Thread.sleep(1); } catch (InterruptedException ignored) {}
}
}, "writer");
Thread[] readers = new Thread[4];
for (int i = 0; i < readers.length; i++) {
final int id = i;
readers[i] = new Thread(() -> {
double sum = 0;
for (int k = 0; k < 5_000; k++) sum += pt.distanceFromOrigin();
System.out.println(" reader " + id + " sum = " + sum);
}, "reader-" + i);
}
writer.start();
for (Thread t : readers) t.start();
writer.join();
for (Thread t : readers) t.join();
section("3) tryConvertToWriteLock (upgrade) — allowed once you hold a read stamp");
Point pp = new Point();
pp.moveIfAtOrigin(7, 9);
System.out.println("after conditional move: distance = " + pp.distanceFromOrigin());
section("4) StampedLock is NOT reentrant");
StampedLock sl = new StampedLock();
long s = sl.writeLock();
// long s2 = sl.writeLock(); // <-- DEADLOCKS (same thread)
sl.unlockWrite(s);
System.out.println("released exclusive stamp");
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}