-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSimulatedCAS.java
More file actions
34 lines (28 loc) · 788 Bytes
/
SimulatedCAS.java
File metadata and controls
34 lines (28 loc) · 788 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
28
29
30
31
32
33
34
package net.jcip.examples;
import net.jcip.annotations.*;
/**
* SimulatedCAS
* <p/>
* Simulated CAS operation
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class SimulatedCAS {
@GuardedBy("this") private int value;
public synchronized int get() {
return value;
}
public synchronized int compareAndSwap(int expectedValue,
int newValue) {
int oldValue = value;
if (oldValue == expectedValue)
value = newValue;
return oldValue;
}
public synchronized boolean compareAndSet(int expectedValue,
int newValue) {
return (expectedValue
== compareAndSwap(expectedValue, newValue));
}
}