16 |
|
return new TestSuite(AtomicMarkableReferenceTest.class); |
17 |
|
} |
18 |
|
|
19 |
– |
static final Integer zero = new Integer(0); |
20 |
– |
static final Integer one = new Integer(1); |
21 |
– |
static final Integer two = new Integer(2); |
22 |
– |
static final Integer m3 = new Integer(-3); |
23 |
– |
|
19 |
|
/** |
20 |
< |
* |
20 |
> |
* constructeor initializes to given reference and mark |
21 |
|
*/ |
22 |
|
public void testConstructor(){ |
23 |
|
AtomicMarkableReference ai = new AtomicMarkableReference(one, false); |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* |
33 |
> |
* get returns the last values of reference and mark set |
34 |
|
*/ |
35 |
|
public void testGetSet(){ |
36 |
|
boolean[] mark = new boolean[1]; |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* |
55 |
> |
* attemptMark succeeds in single thread |
56 |
|
*/ |
57 |
|
public void testAttemptMark(){ |
58 |
|
boolean[] mark = new boolean[1]; |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* |
68 |
> |
* compareAndSet succeeds in changing values if equal to expected reference |
69 |
> |
* and mark else fails |
70 |
|
*/ |
71 |
|
public void testCompareAndSet(){ |
72 |
|
boolean[] mark = new boolean[1]; |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* |
92 |
> |
* compareAndSet in one thread enables another waiting for reference value |
93 |
> |
* to succeed |
94 |
> |
*/ |
95 |
> |
public void testCompareAndSetInMultipleThreads() { |
96 |
> |
final AtomicMarkableReference ai = new AtomicMarkableReference(one, false); |
97 |
> |
Thread t = new Thread(new Runnable() { |
98 |
> |
public void run() { |
99 |
> |
while(!ai.compareAndSet(two, three, false, false)) Thread.yield(); |
100 |
> |
}}); |
101 |
> |
try { |
102 |
> |
t.start(); |
103 |
> |
assertTrue(ai.compareAndSet(one, two, false, false)); |
104 |
> |
t.join(LONG_DELAY_MS); |
105 |
> |
assertFalse(t.isAlive()); |
106 |
> |
assertEquals(ai.getReference(), three); |
107 |
> |
assertFalse(ai.isMarked()); |
108 |
> |
} |
109 |
> |
catch(Exception e) { |
110 |
> |
unexpectedException(); |
111 |
> |
} |
112 |
> |
} |
113 |
> |
|
114 |
> |
/** |
115 |
> |
* compareAndSet in one thread enables another waiting for mark value |
116 |
> |
* to succeed |
117 |
> |
*/ |
118 |
> |
public void testCompareAndSetInMultipleThreads2() { |
119 |
> |
final AtomicMarkableReference ai = new AtomicMarkableReference(one, false); |
120 |
> |
Thread t = new Thread(new Runnable() { |
121 |
> |
public void run() { |
122 |
> |
while(!ai.compareAndSet(one, one, true, false)) Thread.yield(); |
123 |
> |
}}); |
124 |
> |
try { |
125 |
> |
t.start(); |
126 |
> |
assertTrue(ai.compareAndSet(one, one, false, true)); |
127 |
> |
t.join(LONG_DELAY_MS); |
128 |
> |
assertFalse(t.isAlive()); |
129 |
> |
assertEquals(ai.getReference(), one); |
130 |
> |
assertFalse(ai.isMarked()); |
131 |
> |
} |
132 |
> |
catch(Exception e) { |
133 |
> |
unexpectedException(); |
134 |
> |
} |
135 |
> |
} |
136 |
> |
|
137 |
> |
/** |
138 |
> |
* repeated weakCompareAndSet succeeds in changing values when equal |
139 |
> |
* to expected |
140 |
|
*/ |
141 |
|
public void testWeakCompareAndSet(){ |
142 |
|
boolean[] mark = new boolean[1]; |