17 |
|
public static void main(String[] args){ |
18 |
|
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
– |
|
21 |
– |
|
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
25 |
< |
* |
25 |
> |
* Contruction with non-existent field throws RuntimeException |
26 |
|
*/ |
27 |
|
public void testConstructor(){ |
28 |
|
try{ |
36 |
|
|
37 |
|
|
38 |
|
/** |
39 |
< |
* |
39 |
> |
* construction with field not of given type throws RuntimeException |
40 |
|
*/ |
41 |
|
public void testConstructor2(){ |
42 |
|
try{ |
49 |
|
} |
50 |
|
|
51 |
|
/** |
52 |
< |
* |
52 |
> |
* Constructor with non-volatile field throws exception |
53 |
|
*/ |
54 |
|
public void testConstructor3(){ |
55 |
|
try{ |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* |
65 |
> |
* get returns the last value set or assigned |
66 |
|
*/ |
67 |
|
public void testGetSet(){ |
68 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
75 |
|
|
76 |
|
} |
77 |
|
/** |
78 |
< |
* |
78 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
79 |
|
*/ |
80 |
|
public void testCompareAndSet(){ |
81 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* |
93 |
> |
* compareAndSet in one thread enables another waiting for value |
94 |
> |
* to succeed |
95 |
> |
*/ |
96 |
> |
public void testCompareAndSetInMultipleThreads() { |
97 |
> |
x = one; |
98 |
> |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
99 |
> |
|
100 |
> |
Thread t = new Thread(new Runnable() { |
101 |
> |
public void run() { |
102 |
> |
while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); |
103 |
> |
}}); |
104 |
> |
try { |
105 |
> |
t.start(); |
106 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
107 |
> |
t.join(LONG_DELAY_MS); |
108 |
> |
assertFalse(t.isAlive()); |
109 |
> |
assertEquals(a.get(this), three); |
110 |
> |
} |
111 |
> |
catch(Exception e) { |
112 |
> |
unexpectedException(); |
113 |
> |
} |
114 |
> |
} |
115 |
> |
|
116 |
> |
/** |
117 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
118 |
> |
* to expected |
119 |
|
*/ |
120 |
|
public void testWeakCompareAndSet(){ |
121 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
< |
* |
131 |
> |
* getAndSet returns previous value and sets to given value |
132 |
|
*/ |
133 |
|
public void testGetAndSet(){ |
134 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |