18 |
|
} |
19 |
|
|
20 |
|
/** |
21 |
< |
* |
21 |
> |
* constructor initializes to given value |
22 |
|
*/ |
23 |
|
public void testConstructor(){ |
24 |
|
AtomicReference ai = new AtomicReference(one); |
26 |
|
} |
27 |
|
|
28 |
|
/** |
29 |
< |
* |
29 |
> |
* default constructed initializes to null |
30 |
|
*/ |
31 |
|
public void testConstructor2(){ |
32 |
|
AtomicReference ai = new AtomicReference(); |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
< |
* |
37 |
> |
* get returns the last value set |
38 |
|
*/ |
39 |
|
public void testGetSet(){ |
40 |
|
AtomicReference ai = new AtomicReference(one); |
46 |
|
|
47 |
|
} |
48 |
|
/** |
49 |
< |
* |
49 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
50 |
|
*/ |
51 |
|
public void testCompareAndSet(){ |
52 |
|
AtomicReference ai = new AtomicReference(one); |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* |
63 |
> |
* compareAndSet in one thread enables another waiting for value |
64 |
> |
* to succeed |
65 |
> |
*/ |
66 |
> |
public void testCompareAndSetInMultipleThreads() { |
67 |
> |
final AtomicReference ai = new AtomicReference(one); |
68 |
> |
Thread t = new Thread(new Runnable() { |
69 |
> |
public void run() { |
70 |
> |
while(!ai.compareAndSet(two, three)) Thread.yield(); |
71 |
> |
}}); |
72 |
> |
try { |
73 |
> |
t.start(); |
74 |
> |
assertTrue(ai.compareAndSet(one, two)); |
75 |
> |
t.join(LONG_DELAY_MS); |
76 |
> |
assertFalse(t.isAlive()); |
77 |
> |
assertEquals(ai.get(), three); |
78 |
> |
} |
79 |
> |
catch(Exception e) { |
80 |
> |
unexpectedException(); |
81 |
> |
} |
82 |
> |
} |
83 |
> |
|
84 |
> |
/** |
85 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
86 |
> |
* to expected |
87 |
|
*/ |
88 |
|
public void testWeakCompareAndSet(){ |
89 |
|
AtomicReference ai = new AtomicReference(one); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* |
98 |
> |
* getAndSet returns previous value and sets to given value |
99 |
|
*/ |
100 |
|
public void testGetAndSet(){ |
101 |
|
AtomicReference ai = new AtomicReference(one); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* |
108 |
> |
* a deserialized serialized atomic holds same value |
109 |
|
*/ |
110 |
|
public void testSerialization() { |
111 |
|
AtomicReference l = new AtomicReference(); |