41 |
|
try { |
42 |
|
new Phaser(-1); |
43 |
|
shouldThrow(); |
44 |
< |
} catch (IllegalArgumentException success) { |
45 |
< |
} |
44 |
> |
} catch (IllegalArgumentException success) {} |
45 |
|
} |
46 |
|
|
47 |
|
/** |
61 |
|
try { |
62 |
|
new Phaser(new Phaser(), -1); |
63 |
|
shouldThrow(); |
64 |
< |
} catch (IllegalArgumentException success) { |
66 |
< |
} |
64 |
> |
} catch (IllegalArgumentException success) {} |
65 |
|
} |
66 |
|
|
67 |
|
/** |
98 |
|
try { |
99 |
|
phaser.register(); |
100 |
|
shouldThrow(); |
101 |
< |
} catch (IllegalStateException success) { |
104 |
< |
} |
101 |
> |
} catch (IllegalStateException success) {} |
102 |
|
} |
103 |
|
|
104 |
|
/** |
138 |
|
try { |
139 |
|
new Phaser().bulkRegister(-1); |
140 |
|
shouldThrow(); |
141 |
< |
} catch (IllegalArgumentException success) { |
145 |
< |
} |
141 |
> |
} catch (IllegalArgumentException success) {} |
142 |
|
} |
143 |
|
|
144 |
|
/** |
159 |
|
try { |
160 |
|
new Phaser().bulkRegister(1 << 16); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (IllegalStateException success) { |
167 |
< |
} |
162 |
> |
} catch (IllegalStateException success) {} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* Arrive() on a registered phaser increments phase. |
179 |
> |
* Arrive() on a registered phaser increments phase. |
180 |
|
*/ |
181 |
|
public void testArrive1() { |
182 |
|
Phaser phaser = new Phaser(1); |
194 |
|
for (int i = 0; i < 10; i++) |
195 |
|
phaser.register(); |
196 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
197 |
< |
void realRun() throws InterruptedException { |
197 |
> |
public void realRun() throws InterruptedException { |
198 |
|
Thread.sleep(SMALL_DELAY_MS); |
199 |
|
phaser.arriveAndDeregister(); |
200 |
|
}})); |
224 |
|
Phaser phaser = new Phaser(); |
225 |
|
phaser.arriveAndDeregister(); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (IllegalStateException success) { |
233 |
< |
} |
227 |
> |
} catch (IllegalStateException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
235 |
|
phaser.register(); |
236 |
|
phaser.arrive(); |
237 |
|
int p = phaser.getArrivedParties(); |
238 |
< |
assertTrue(p == 1); |
238 |
> |
assertEquals(1, p); |
239 |
|
phaser.arriveAndDeregister(); |
240 |
|
assertTrue(phaser.getArrivedParties() < p); |
241 |
|
} |
252 |
|
assertTrue(parent.getUnarrivedParties() > 0); |
253 |
|
assertTrue(root.getUnarrivedParties() > 0); |
254 |
|
root.arriveAndDeregister(); |
255 |
< |
assertTrue(parent.getUnarrivedParties() == 0); |
256 |
< |
assertTrue(root.getUnarrivedParties() == 0); |
255 |
> |
assertEquals(0, parent.getUnarrivedParties()); |
256 |
> |
assertEquals(0, root.getUnarrivedParties()); |
257 |
|
assertTrue(root.isTerminated() && parent.isTerminated()); |
258 |
|
} |
259 |
|
|
283 |
|
assertTrue(child.getUnarrivedParties() > 0); |
284 |
|
root.register(); |
285 |
|
root.arriveAndDeregister(); |
286 |
< |
assertTrue(parent.getUnarrivedParties() == 0); |
287 |
< |
assertTrue(child.getUnarrivedParties() == 0); |
286 |
> |
assertEquals(0, parent.getUnarrivedParties()); |
287 |
> |
assertEquals(0, child.getUnarrivedParties()); |
288 |
|
assertTrue(root.isTerminated()); |
289 |
|
} |
290 |
|
|
295 |
|
public void testArriveAndDeregister6() throws InterruptedException { |
296 |
|
final Phaser phaser = new Phaser(2); |
297 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
< |
void realRun() { |
298 |
> |
public void realRun() { |
299 |
|
sleepTillInterrupted(SHORT_DELAY_MS); |
300 |
|
phaser.arrive(); |
301 |
|
}}); |
330 |
|
phaser.register(); |
331 |
|
|
332 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
333 |
< |
void realRun() throws InterruptedException { |
333 |
> |
public void realRun() throws InterruptedException { |
334 |
|
phaser.register(); |
335 |
|
sleepTillInterrupted(LONG_DELAY_MS); |
336 |
|
phaser.awaitAdvance(phaser.arrive()); |
353 |
|
List<Thread> threads = new ArrayList<Thread>(); |
354 |
|
for (int i = 0; i < 4; i++) { |
355 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
356 |
< |
void realRun() { |
356 |
> |
public void realRun() { |
357 |
|
int phase = phaser.arrive(); |
358 |
|
phaseCount.incrementAndGet(); |
359 |
|
sleepTillInterrupted(SMALL_DELAY_MS); |
360 |
|
phaser.awaitAdvance(phase); |
361 |
< |
threadAssertTrue(phaseCount.get() == 4); |
361 |
> |
assertEquals(phaseCount.get(), 4); |
362 |
|
}})); |
363 |
|
} |
364 |
|
for (Thread thread : threads) |
376 |
|
List<Thread> threads = new ArrayList<Thread>(); |
377 |
|
for (int i = 0; i < 8; i++) { |
378 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
379 |
< |
void realRun() { |
379 |
> |
public void realRun() { |
380 |
|
sleepTillInterrupted(SHORT_DELAY_MS); |
381 |
|
phaser.arrive(); |
382 |
|
}})); |
383 |
|
phase = phaser.awaitAdvance(phaser.arrive()); |
384 |
< |
threadAssertEquals(phase, phaser.getPhase()); |
384 |
> |
assertEquals(phase, phaser.getPhase()); |
385 |
|
} |
386 |
|
for (Thread thread : threads) |
387 |
|
thread.join(); |
399 |
|
* waits for the second thread's party to arrive |
400 |
|
*/ |
401 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
402 |
< |
void realRun() { |
402 |
> |
public void realRun() { |
403 |
|
sleepTillInterrupted(SMALL_DELAY_MS); |
404 |
|
int phase = phaser.awaitAdvance(phaser.arrive()); |
405 |
|
/* |
406 |
|
* This point is reached when force termination is called in which phase = -1 |
407 |
|
*/ |
408 |
< |
threadAssertTrue(phase < 0); |
409 |
< |
threadAssertTrue(phaser.isTerminated()); |
408 |
> |
assertTrue(phase < 0); |
409 |
> |
assertTrue(phaser.isTerminated()); |
410 |
|
}}); |
411 |
|
/* |
412 |
|
* This thread will cause the first thread run to wait, in doing so |
414 |
|
* should exit peacefully as this one |
415 |
|
*/ |
416 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
417 |
< |
void realRun() { |
417 |
> |
public void realRun() { |
418 |
|
sleepTillInterrupted(MEDIUM_DELAY_MS); |
419 |
|
int p1 = phaser.arrive(); |
420 |
|
int phase = phaser.awaitAdvance(p1); |
421 |
< |
threadAssertTrue(phase < 0); |
422 |
< |
threadAssertTrue(phaser.isTerminated()); |
421 |
> |
assertTrue(phase < 0); |
422 |
> |
assertTrue(phaser.isTerminated()); |
423 |
|
}}); |
424 |
|
|
425 |
|
phaser.arrive(); |
437 |
|
Phaser phaser = new Phaser(); |
438 |
|
phaser.arriveAndAwaitAdvance(); |
439 |
|
shouldThrow(); |
440 |
< |
} catch (IllegalStateException success) { |
447 |
< |
} |
440 |
> |
} catch (IllegalStateException success) {} |
441 |
|
} |
442 |
|
|
443 |
|
/** |
446 |
|
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
447 |
|
final Phaser phaser = new Phaser(2); |
448 |
|
Thread th = newStartedThread(new CheckedRunnable() { |
449 |
< |
void realRun() { |
449 |
> |
public void realRun() { |
450 |
|
phaser.arriveAndAwaitAdvance(); |
451 |
|
}}); |
452 |
|
|
465 |
|
*/ |
466 |
|
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
467 |
|
final Phaser phaser = new Phaser(1); |
475 |
– |
final AtomicInteger arrivingCount = new AtomicInteger(0); |
468 |
|
final List<Thread> threads = new ArrayList<Thread>(); |
469 |
< |
for (int i = 0; i < 6; i++) { |
469 |
> |
for (int i = 0; i < 3; i++) { |
470 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
471 |
< |
void realRun() throws InterruptedException { |
472 |
< |
phaser.register(); |
473 |
< |
sleepTillInterrupted(SHORT_DELAY_MS); |
474 |
< |
arrivingCount.getAndIncrement(); |
483 |
< |
phaser.arrive(); |
484 |
< |
}})); |
471 |
> |
public void realRun() throws InterruptedException { |
472 |
> |
phaser.register(); |
473 |
> |
phaser.arriveAndAwaitAdvance(); |
474 |
> |
}})); |
475 |
|
} |
476 |
< |
int phaseNumber = phaser.arriveAndAwaitAdvance(); |
477 |
< |
arrivingCount.incrementAndGet(); |
478 |
< |
//the + 1 adds to expectedArrive to account for the main threads arrival |
489 |
< |
int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1; |
490 |
< |
threadAssertEquals(expectedArrived, arrivingCount.get()); |
476 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
477 |
> |
assertEquals(phaser.getArrivedParties(), 3); |
478 |
> |
phaser.arriveAndAwaitAdvance(); |
479 |
|
for (Thread thread : threads) |
480 |
|
thread.join(); |
481 |
|
} |