equal
deleted
inserted
replaced
40 import java.util.concurrent.CyclicBarrier; |
40 import java.util.concurrent.CyclicBarrier; |
41 import java.util.concurrent.ExecutorService; |
41 import java.util.concurrent.ExecutorService; |
42 import java.util.concurrent.Executors; |
42 import java.util.concurrent.Executors; |
43 import java.util.concurrent.ThreadLocalRandom; |
43 import java.util.concurrent.ThreadLocalRandom; |
44 import java.util.concurrent.TimeoutException; |
44 import java.util.concurrent.TimeoutException; |
45 import java.util.concurrent.atomic.AtomicBoolean; |
|
46 import java.util.concurrent.atomic.AtomicInteger; |
45 import java.util.concurrent.atomic.AtomicInteger; |
47 |
46 |
48 import junit.framework.Test; |
47 import junit.framework.Test; |
49 import junit.framework.TestSuite; |
48 import junit.framework.TestSuite; |
50 |
49 |
318 }}); |
317 }}); |
319 |
318 |
320 c.await(); |
319 c.await(); |
321 awaitTermination(t1); |
320 awaitTermination(t1); |
322 awaitTermination(t2); |
321 awaitTermination(t2); |
323 } |
|
324 |
|
325 /** |
|
326 * All threads block while a barrier is broken. |
|
327 */ |
|
328 public void testReset_Leakage() throws InterruptedException { |
|
329 final CyclicBarrier c = new CyclicBarrier(2); |
|
330 final AtomicBoolean done = new AtomicBoolean(); |
|
331 Thread t = newStartedThread(new CheckedRunnable() { |
|
332 public void realRun() { |
|
333 while (!done.get()) { |
|
334 try { |
|
335 while (c.isBroken()) |
|
336 c.reset(); |
|
337 |
|
338 c.await(); |
|
339 shouldThrow(); |
|
340 } |
|
341 catch (BrokenBarrierException | InterruptedException ok) {} |
|
342 }}}); |
|
343 |
|
344 for (int i = 0; i < 4; i++) { |
|
345 delay(timeoutMillis()); |
|
346 t.interrupt(); |
|
347 } |
|
348 done.set(true); |
|
349 t.interrupt(); |
|
350 awaitTermination(t); |
|
351 } |
322 } |
352 |
323 |
353 /** |
324 /** |
354 * Reset of a non-broken barrier does not break barrier |
325 * Reset of a non-broken barrier does not break barrier |
355 */ |
326 */ |
503 final CyclicBarrier barrier = |
474 final CyclicBarrier barrier = |
504 new CyclicBarrier(parties, () -> tripCount.getAndIncrement()); |
475 new CyclicBarrier(parties, () -> tripCount.getAndIncrement()); |
505 final ExecutorService e = Executors.newFixedThreadPool(nTasks); |
476 final ExecutorService e = Executors.newFixedThreadPool(nTasks); |
506 final Runnable awaiter = () -> { |
477 final Runnable awaiter = () -> { |
507 try { |
478 try { |
508 if (ThreadLocalRandom.current().nextBoolean()) |
479 if (randomBoolean()) |
509 barrier.await(); |
480 barrier.await(); |
510 else |
481 else |
511 barrier.await(LONG_DELAY_MS, MILLISECONDS); |
482 barrier.await(LONG_DELAY_MS, MILLISECONDS); |
512 awaitCount.getAndIncrement(); |
483 awaitCount.getAndIncrement(); |
513 } catch (Throwable fail) { threadUnexpectedException(fail); }}; |
484 } catch (Throwable fail) { threadUnexpectedException(fail); }}; |