MCPcopy Create free account
hub / github.com/ReadyTalk/avian / runAtomicReferenceTest

Method runAtomicReferenceTest

test/AtomicTests.java:147–189  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

145 }
146
147 private static void runAtomicReferenceTest() {
148 final AtomicReference<Integer> result = new AtomicReference<Integer>(0);
149 final AtomicInteger threadDoneCount = new AtomicInteger(0);
150 // only using an AtomicBoolean here so I don't need two variables to do the synchronize/wait/notify
151 final AtomicBoolean threadsStart = new AtomicBoolean(false);
152
153 Runnable operationRunnable = new Runnable() {
154 @Override
155 public void run() {
156 for (int i = 0; i < iterationsPerThread; i++) {
157 Integer current = result.get();
158 while (! result.compareAndSet(current, current + 1)) {
159 current = result.get();
160 }
161 }
162 }
163 };
164
165 for (int i = 0; i < threadCount; i++) {
166 new Thread(new DelayedRunnable(threadsStart,
167 operationRunnable,
168 threadDoneCount)).start();
169 }
170
171 synchronized (threadsStart) {
172 threadsStart.set(true);
173
174 threadsStart.notifyAll();
175 }
176
177 try {
178 blockTillThreadsDone(threadDoneCount);
179 } catch (InterruptedException e) {
180 // let thread exit
181 return;
182 }
183
184 long expectedResult = threadCount * iterationsPerThread;
185 Integer resultValue = result.get();
186 if (resultValue != expectedResult) {
187 throw new IllegalStateException(resultValue + " != " + expectedResult);
188 }
189 }
190
191 private static class DelayedRunnable implements Runnable {
192 private final AtomicBoolean threadsStart;

Callers 1

mainMethod · 0.95

Calls 5

setMethod · 0.95
blockTillThreadsDoneMethod · 0.95
getMethod · 0.95
startMethod · 0.45
notifyAllMethod · 0.45

Tested by

no test coverage detected