Java Code Examples for java.util.concurrent.Phaser#forceTermination()
The following examples show how to use
java.util.concurrent.Phaser#forceTermination() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhaserTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * awaitAdvance returns when the phaser is externally terminated */ public void testAwaitAdvance6() { final Phaser phaser = new Phaser(3); final CountDownLatch pleaseForceTermination = new CountDownLatch(2); final List<Thread> threads = new ArrayList<>(); for (int i = 0; i < 2; i++) { Runnable r = new CheckedRunnable() { public void realRun() { assertEquals(0, phaser.arrive()); pleaseForceTermination.countDown(); assertTrue(phaser.awaitAdvance(0) < 0); assertTrue(phaser.isTerminated()); assertTrue(phaser.getPhase() < 0); assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); assertEquals(3, phaser.getRegisteredParties()); }}; threads.add(newStartedThread(r)); } await(pleaseForceTermination); phaser.forceTermination(); assertTrue(phaser.isTerminated()); assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); for (Thread thread : threads) awaitTermination(thread); assertEquals(3, phaser.getRegisteredParties()); }
Example 2
Source File: TieredArriveLoops.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void test(String[] args) throws Throwable { final Phaser parent = new Phaser(); final Phaser child1 = new Phaser(parent); final Phaser child2 = new Phaser(parent); Thread t1 = new Thread(runner(child1)); Thread t2 = new Thread(runner(child2)); t1.start(); t2.start(); for (int prevPhase = 0, phase; ; prevPhase = phase) { phase = child2.getPhase(); check(phase >= prevPhase); if (System.nanoTime() - quittingTimeNanos > 0) { System.err.printf("phase=%d%n", phase); child1.forceTermination(); break; } } t1.join(); t2.join(); }
Example 3
Source File: PhaserTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * awaitAdvance returns when the phaser is externally terminated */ public void testAwaitAdvance6() { final Phaser phaser = new Phaser(3); final CountDownLatch pleaseForceTermination = new CountDownLatch(2); final List<Thread> threads = new ArrayList<Thread>(); for (int i = 0; i < 2; i++) { Runnable r = new CheckedRunnable() { public void realRun() { assertEquals(0, phaser.arrive()); pleaseForceTermination.countDown(); assertTrue(phaser.awaitAdvance(0) < 0); assertTrue(phaser.isTerminated()); assertTrue(phaser.getPhase() < 0); assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); assertEquals(3, phaser.getRegisteredParties()); }}; threads.add(newStartedThread(r)); } await(pleaseForceTermination); phaser.forceTermination(); assertTrue(phaser.isTerminated()); assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); for (Thread thread : threads) awaitTermination(thread); assertEquals(3, phaser.getRegisteredParties()); }
Example 4
Source File: PhaserTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * arrive() returns a negative number if the Phaser is terminated */ public void testArrive3() { Phaser phaser = new Phaser(1); phaser.forceTermination(); assertTerminated(phaser, 0, 1); assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); assertTrue(phaser.arrive() < 0); assertTrue(phaser.register() < 0); assertTrue(phaser.arriveAndDeregister() < 0); assertTrue(phaser.awaitAdvance(1) < 0); assertTrue(phaser.getPhase() < 0); }
Example 5
Source File: PhaserTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * arrive() returns a negative number if the Phaser is terminated */ public void testArrive3() { Phaser phaser = new Phaser(1); phaser.forceTermination(); assertTerminated(phaser, 0, 1); assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); assertTrue(phaser.arrive() < 0); assertTrue(phaser.register() < 0); assertTrue(phaser.arriveAndDeregister() < 0); assertTrue(phaser.awaitAdvance(1) < 0); assertTrue(phaser.getPhase() < 0); }
Example 6
Source File: ManagedExecutorTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * Verify that the ManagedExecutor implementation starts 2 async tasks/actions, and no more, * when maxAsync is configured to 2. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test public void maxAsync2() throws ExecutionException, InterruptedException, TimeoutException { ManagedExecutor executor = ManagedExecutor.builder() .maxAsync(2) .propagated() .cleared(ThreadContext.ALL_REMAINING) .build(); Phaser barrier = new Phaser(2); try { // Use up both maxAsync slots on blocking operations and wait for them to start Future<Integer> future1 = executor.submit(() -> barrier.awaitAdvance(barrier.arriveAndAwaitAdvance())); CompletableFuture<Integer> future2 = executor.supplyAsync(() -> barrier.awaitAdvance(barrier.arriveAndAwaitAdvance())); barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS); // This data structure holds the results of tasks which shouldn't be able to run yet LinkedBlockingQueue<String> results = new LinkedBlockingQueue<String>(); // Submit additional tasks/actions for async execution. // These should queue, but otherwise be unable to start yet due to maxAsync=2. CompletableFuture<Void> future3 = executor.runAsync(() -> results.offer("Result3")); CompletableFuture<Boolean> future4 = executor.supplyAsync(() -> results.offer("Result4")); Future<Boolean> future5 = executor.submit(() -> results.offer("Result5")); CompletableFuture<Boolean> future6 = executor.completedFuture("6") .thenApplyAsync(s -> results.offer("Result" + s)); // Detect whether any of the above tasks/actions run within the next 5 seconds Assert.assertNull(results.poll(5, TimeUnit.SECONDS), "Should not be able start more than 2 async tasks when maxAsync is 2."); // unblock and allow tasks to finish barrier.arrive(); barrier.arrive(); // there are 2 parties in each phase Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "None of the queued tasks ran."); Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 1 of the queued tasks ran."); Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 2 of the queued tasks ran."); Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 3 of the queued tasks ran."); Assert.assertEquals(future1.get(), Integer.valueOf(2), "Unexpected result of first task."); Assert.assertEquals(future2.get(), Integer.valueOf(2), "Unexpected result of second task."); Assert.assertNull(future3.join(), "Unexpected result of third task."); Assert.assertEquals(future4.join(), Boolean.TRUE, "Unexpected result of fourth task."); Assert.assertEquals(future5.get(), Boolean.TRUE, "Unexpected result of fifth task."); Assert.assertEquals(future6.get(), Boolean.TRUE, "Unexpected result of sixth task."); } finally { barrier.forceTermination(); executor.shutdownNow(); } }
Example 7
Source File: MPConfigTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * Verify that the maxAsync and maxQueued attributes of a ManagedExecutor are defaulted * according to the defaults specified by the application in MicroProfile Config. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test(dependsOnMethods = "beanInjected") public void defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig() throws ExecutionException, InterruptedException, TimeoutException { // Expected config is maxAsync=1, maxQueued=4; propagated=Buffer; cleared=Remaining ManagedExecutor executor = ManagedExecutor.builder().propagated(Buffer.CONTEXT_NAME).build(); Phaser barrier = new Phaser(1); try { // Set non-default values Buffer.set(new StringBuffer("defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig-test-buffer")); Label.set("defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig-test-label"); // First, use up the single maxAsync slot with a blocking task and wait for it to start executor.submit(() -> barrier.awaitAdvanceInterruptibly(barrier.arrive() + 1)); barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS); // Use up queue positions CompletableFuture<String> cf1 = executor.supplyAsync(() -> Buffer.get().toString()); CompletableFuture<String> cf2 = executor.supplyAsync(() -> Label.get()); CompletableFuture<String> cf3 = executor.supplyAsync(() -> "III"); CompletableFuture<String> cf4 = executor.supplyAsync(() -> "IV"); // Fifth attempt to queue a task must be rejected try { CompletableFuture<String> cf5 = executor.supplyAsync(() -> "V"); // CompletableFuture interface does not provide detail on precisely how to report rejection, // so tolerate both possibilities: exception raised or stage returned with exceptional completion. Assert.assertTrue(cf5.isDone() && cf5.isCompletedExceptionally(), "Exceeded maxQueued of 4. Future for 5th queued task/action is " + cf5); } catch (RejectedExecutionException x) { // test passes } // unblock and allow tasks to finish barrier.arrive(); Assert.assertEquals(cf1.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig-test-buffer", "First task: Context not propagated as configured on the builder."); Assert.assertEquals(cf2.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "", "Second task: Context not cleared as defaulted by MicroProfile Config property."); Assert.assertEquals(cf3.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "III", "Unexpected result of third task."); Assert.assertEquals(cf4.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "IV", "Unexpected result of fourth task."); } finally { barrier.forceTermination(); // Restore original values Buffer.set(null); Label.set(null); } }
Example 8
Source File: MPConfigTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * Verify that MicroProfile config defaults the maxAsync attribute and honors the explicitly specified * maxQueued attribute, when only the propagated and maxQueued attributes are specified by the application. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test(dependsOnMethods = "beanInjected") public void explicitlySpecifyMaxQueued5() throws ExecutionException, InterruptedException, TimeoutException { // Expected config is maxAsync=1; maxQueued=5; propagated={}; cleared=Remaining Assert.assertNotNull(producedExecutor, "Injection failure. Cannot run test."); Phaser barrier = new Phaser(1); try { // First, use up the single maxAsync slot with a blocking task and wait for it to start producedExecutor.submit(() -> barrier.awaitAdvanceInterruptibly(barrier.arrive() + 1)); barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS); // Use up queue positions CompletableFuture<String> future1 = producedExecutor.supplyAsync(() -> "Q_1"); CompletableFuture<String> future2 = producedExecutor.supplyAsync(() -> "Q_2"); CompletableFuture<String> future3 = producedExecutor.supplyAsync(() -> "Q_3"); Future<String> future4 = producedExecutor.submit(() -> "Q_4"); Future<String> future5 = producedExecutor.submit(() -> "Q_5"); // Sixth attempt to queue a task must be rejected try { Future<String> future6 = producedExecutor.submit(() -> "Q_6"); Assert.fail("Exceeded maxQueued of 5. Future for 6th queued task/action is " + future6); } catch (RejectedExecutionException x) { // test passes } // unblock and allow tasks to finish barrier.arrive(); Assert.assertEquals(future1.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_1", "Unexpected result of first task."); Assert.assertEquals(future2.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_2", "Unexpected result of second task."); Assert.assertEquals(future3.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_3", "Unexpected result of third task."); Assert.assertEquals(future4.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_4", "Unexpected result of fourth task."); Assert.assertEquals(future5.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_5", "Unexpected result of fifth task."); } finally { barrier.forceTermination(); } }