public void testCustomScheduler_deadlock() throws InterruptedException, BrokenBarrierException { final CyclicBarrier inGetNextSchedule = new CyclicBarrier(2); // This will flakily deadlock, so run it multiple times to increase the flake likelihood for (int i = 0; i < 1000; i++) { Service service = new AbstractScheduledService() { @Override protected void runOneIteration() {} @Override protected Scheduler scheduler() { return new CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { if (state() != State.STARTING) { inGetNextSchedule.await(); Thread.yield(); throw new RuntimeException("boom"); } return new Schedule(0, TimeUnit.NANOSECONDS); } }; } }; service.startAsync().awaitRunning(); inGetNextSchedule.await(); service.stopAsync(); } }
public void await() { if(0 == delay) { log.info("No pause between retry"); return; } final Timer wakeup = new Timer(); final CyclicBarrier wait = new CyclicBarrier(2); // Schedule for immediate execution with an interval of 1s wakeup.scheduleAtFixedRate(new PauserTimerTask(wait), 0, 1000); try { // Wait for notify from wakeup timer wait.await(); } catch(InterruptedException | BrokenBarrierException e) { log.error(e.getMessage(), e); } }
@Test public void testConcurrent() throws Exception { final TransferQueue queue = new TransferQueue(1); final DownloadTransfer transfer = new DownloadTransfer(new Host(new TestProtocol()), new Path("/t", EnumSet.of(Path.Type.directory)), null); queue.add(transfer, new DisabledProgressListener()); final AtomicBoolean added = new AtomicBoolean(); final CyclicBarrier wait = new CyclicBarrier(2); new Thread(new Runnable() { @Override public void run() { queue.add(new DownloadTransfer(new Host(new TestProtocol()), new Path("/t", EnumSet.of(Path.Type.directory)), null), new DisabledProgressListener()); added.set(true); try { wait.await(); } catch(InterruptedException | BrokenBarrierException e) { fail(); } } }).start(); assertFalse(added.get()); queue.remove(transfer); wait.await(); assertTrue(added.get()); }
@Test public void testTxConflictHandling() throws InterruptedException, BrokenBarrierException, TimeoutException, IOException { // Test creation of user in current thread int nFriendsBefore; try (Tx tx = graph.tx()) { p = addPersonWithFriends(tx.getGraph(), "Person2"); manipulatePerson(tx.getGraph(), p); tx.success(); nFriendsBefore = p.getFriends().size(); } CyclicBarrier b = new CyclicBarrier(3); addFriendToPersonInThread(p, b); addFriendToPersonInThread(p, b); // Wait until both threads have started their transactions b.await(); Thread.sleep(2000); try (Tx tx = graph.tx()) { // Reload the person in a fresh transaction p = tx.getGraph().getFramedVertexExplicit(Person.class, p.getId()); int nFriendsAfter = p.getFriends().size(); assertEquals(nFriendsBefore + 2, nFriendsAfter); } }
@Override public void run() { int counter; System.out.printf("%s: Processing lines from %d to %d.\n", Thread.currentThread().getName(), firstRow, lastRow); for (int i = firstRow; i < lastRow; i++) { int row[] = mock.getRow(i); counter = 0; for (int aRow : row) { if (aRow == number) { counter++; } } results.setData(i, counter); } System.out.printf("%s: Lines processed.\n", Thread.currentThread().getName()); try { barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } }
/** * 版本3.0.9以下存在多线程初始化问题,这个类作为一个样例 */ public static void main(String[] args) throws InterruptedException, BrokenBarrierException { System.out.println(Arrays.asList(splitWord)); for (int j = 0; j < 1000; j++) { CyclicBarrier barrier = new CyclicBarrier(11, null); for (int i = 0; i < 10; i++) { Thread t = new Thread(new Worker(barrier), "t" + i); t.start(); } Thread.sleep(500); barrier.await(); while (barrier.getNumberWaiting() > 0) { Thread.sleep(1000); } Thread.sleep(1000); System.out.println(Arrays.asList(splitWord)); } }
@SuppressWarnings("unchecked") @Test(timeout=60000) public void testBlockNewContainerRequestsOnStartAndResync() throws IOException, InterruptedException, YarnException { NodeManager nm = new TestNodeManager2(); YarnConfiguration conf = createNMConfig(); conf.setBoolean(YarnConfiguration.RM_WORK_PRESERVING_RECOVERY_ENABLED, false); nm.init(conf); nm.start(); // Start the container in running state ContainerId cId = TestNodeManagerShutdown.createContainerId(); TestNodeManagerShutdown.startContainer(nm, cId, localFS, tmpDir, processStartFile); nm.getNMDispatcher().getEventHandler() .handle(new NodeManagerEvent(NodeManagerEventType.RESYNC)); try { syncBarrier.await(); } catch (BrokenBarrierException e) { } Assert.assertFalse(assertionFailedInThread.get()); nm.stop(); }
@Override public void handle(HttpExchange exchange) throws IOException { count++; try { switch(count) { case 0: AuthenticationHandler.errorReply(exchange, "Basic realm=\"realm1\""); break; case 1: t1Cond1.await(); t1cond2latch.await(); AuthenticationHandler.okReply(exchange); break; default: System.out.println ("Unexpected request"); } } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } }
@Override public void handle(HttpExchange exchange) throws IOException { count++; try { switch(count) { case 0: AuthenticationHandler.errorReply(exchange, "Basic realm=\"realm2\""); break; case 1: t1Cond1.await(); t1cond1latch.countDown(); t1cond2latch.await(); AuthenticationHandler.okReply(exchange); break; default: System.out.println ("Unexpected request"); } } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } }
@Override public void handle(HttpExchange exchange) throws IOException { count++; switch(count) { case 0: AuthenticationHandler.errorReply(exchange, "Basic realm=\"realm1\""); try { t1Cond2.await(); } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } break; case 1: AuthenticationHandler.okReply(exchange); break; default: System.out.println ("Unexpected request"); } }
@Override public void handle(HttpExchange exchange) throws IOException { count++; switch(count) { case 0: AuthenticationHandler.errorReply(exchange, "Basic realm=\"realm2\""); try { t1Cond2.await(); } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } t1cond2latch.countDown(); break; case 1: AuthenticationHandler.okReply(exchange); break; default: System.out.println ("Unexpected request"); } }
public static void main(String[] args) throws Exception { int maxThreads = 5; if (args.length > 0) maxThreads = Integer.parseInt(args[0]); print = true; for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) { System.out.print("Threads: " + i); try { new FutureLoop(i, rnd.split()).test(); } catch (BrokenBarrierException bb) { // OK; ignore } catch (ExecutionException ee) { // OK; ignore } Thread.sleep(TIMEOUT); } pool.shutdown(); if (! pool.awaitTermination(6 * LONG_DELAY_MS, MILLISECONDS)) throw new Error(); }
/** * An interruption in one party causes others waiting in await to * throw BrokenBarrierException */ public void testAwait1_Interrupted_BrokenBarrier() { final CyclicBarrier c = new CyclicBarrier(3); final CountDownLatch pleaseInterrupt = new CountDownLatch(2); Thread t1 = new ThreadShouldThrow(InterruptedException.class) { public void realRun() throws Exception { pleaseInterrupt.countDown(); c.await(); }}; Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { public void realRun() throws Exception { pleaseInterrupt.countDown(); c.await(); }}; t1.start(); t2.start(); await(pleaseInterrupt); t1.interrupt(); awaitTermination(t1); awaitTermination(t2); }
/** * An interruption in one party causes others waiting in timed await to * throw BrokenBarrierException */ public void testAwait2_Interrupted_BrokenBarrier() throws Exception { final CyclicBarrier c = new CyclicBarrier(3); final CountDownLatch pleaseInterrupt = new CountDownLatch(2); Thread t1 = new ThreadShouldThrow(InterruptedException.class) { public void realRun() throws Exception { pleaseInterrupt.countDown(); c.await(LONG_DELAY_MS, MILLISECONDS); }}; Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { public void realRun() throws Exception { pleaseInterrupt.countDown(); c.await(LONG_DELAY_MS, MILLISECONDS); }}; t1.start(); t2.start(); await(pleaseInterrupt); t1.interrupt(); awaitTermination(t1); awaitTermination(t2); }
/** * A reset of an active barrier causes waiting threads to throw * BrokenBarrierException */ public void testReset_BrokenBarrier() throws InterruptedException { final CyclicBarrier c = new CyclicBarrier(3); final CountDownLatch pleaseReset = new CountDownLatch(2); Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { public void realRun() throws Exception { pleaseReset.countDown(); c.await(); }}; Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { public void realRun() throws Exception { pleaseReset.countDown(); c.await(); }}; t1.start(); t2.start(); await(pleaseReset); awaitNumberWaiting(c, 2); c.reset(); awaitTermination(t1); awaitTermination(t2); }
@Override public void handle(HttpExchange exchange) throws IOException { count++; try { switch(count) { case 0: AuthenticationHandler.errorReply(exchange, "Basic realm=\"realm1\""); break; case 1: t1Cond1.await(); AuthenticationHandler.okReply(exchange); break; default: System.out.println ("Unexpected request"); } } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } }
@Override public void handle(HttpExchange exchange) throws IOException { count++; try { switch(count) { case 0: AuthenticationHandler.errorReply(exchange, "Basic realm=\"realm2\""); break; case 1: t1Cond1.await(); AuthenticationHandler.okReply(exchange); break; default: System.out.println ("Unexpected request"); } } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } }
@SuppressWarnings("unchecked") @Test(timeout=60000) public void testBlockNewContainerRequestsOnStartAndResync() throws IOException, InterruptedException, YarnException { NodeManager nm = new TestNodeManager2(); int port = ServerSocketUtil.getPort(49154, 10); YarnConfiguration conf = createNMConfig(port); conf.setBoolean(YarnConfiguration.RM_WORK_PRESERVING_RECOVERY_ENABLED, false); nm.init(conf); nm.start(); // Start the container in running state ContainerId cId = TestNodeManagerShutdown.createContainerId(); TestNodeManagerShutdown.startContainer(nm, cId, localFS, tmpDir, processStartFile, port); nm.getNMDispatcher().getEventHandler() .handle(new NodeManagerEvent(NodeManagerEventType.RESYNC)); try { syncBarrier.await(); } catch (BrokenBarrierException e) { } Assert.assertFalse(assertionFailedInThread.get()); nm.stop(); }
@SuppressWarnings("unchecked") @Test(timeout=60000) public void testContainerResourceIncreaseIsSynchronizedWithRMResync() throws IOException, InterruptedException, YarnException { NodeManager nm = new TestNodeManager4(); YarnConfiguration conf = createNMConfig(); conf.setBoolean( YarnConfiguration.RM_WORK_PRESERVING_RECOVERY_ENABLED, true); nm.init(conf); nm.start(); // Start a container and make sure it is in RUNNING state ((TestNodeManager4)nm).startContainer(); // Simulate a container resource increase in a separate thread ((TestNodeManager4)nm).increaseContainersResource(); // Simulate RM restart by sending a RESYNC event LOG.info("Sending out RESYNC event"); nm.getNMDispatcher().getEventHandler().handle( new NodeManagerEvent(NodeManagerEventType.RESYNC)); try { syncBarrier.await(); } catch (BrokenBarrierException e) { e.printStackTrace(); } Assert.assertFalse(assertionFailedInThread.get()); nm.stop(); }
public static void main(String[] args) throws BrokenBarrierException, InterruptedException { final CyclicBarrier barrier = new CyclicBarrier(4); ExecutorService executorService = Executors.newSingleThreadExecutor(); ListenableFutureTask<String> listenableFutureTask = ListenableFutureTask.create(() -> { log.info("listenable future begin to execute"); TimeUnit.SECONDS.sleep(5); return "listenable future task done"; }); // !!!!! barrier貌似并不能实现,对比下CountDownLatch,两种控制的应用场景 Futures.addCallback(listenableFutureTask, new BarrieredSimpleFutureCallback(0, barrier), executorService); Futures.addCallback(listenableFutureTask, new BarrieredSimpleFutureCallback(1, barrier), executorService); Futures.addCallback(listenableFutureTask, new BarrieredSimpleFutureCallback(2, barrier), executorService); executorService.execute(listenableFutureTask); barrier.await(); executorService.shutdown(); }
public static void main(String[] args) throws InterruptedException, BrokenBarrierException { final CountDownLatch latch = new CountDownLatch(3); //ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()); ExecutorService executorService = Executors.newSingleThreadExecutor(); ListenableFutureTask<String> listenableFutureTask = ListenableFutureTask.create(() -> { log.info("listenable future begin to execute"); TimeUnit.SECONDS.sleep(5); return "listenable future task done"; }); // add three callbacks, use executorService instance to execute Futures.addCallback(listenableFutureTask, new LatchedSimpleFutureCallback(0, latch), executorService); Futures.addCallback(listenableFutureTask, new LatchedSimpleFutureCallback(1, latch), executorService); Futures.addCallback(listenableFutureTask, new LatchedSimpleFutureCallback(2, latch), executorService); // execute listenable future task executorService.execute(listenableFutureTask); // ensure listener execution before ExecutorService#shutdown if (latch.getCount() > 0) { latch.await(); } // ExecutorService.shutdown() executorService.shutdown(); }
private void runDisruptorPass() throws InterruptedException, BrokenBarrierException { final CountDownLatch latch = new CountDownLatch(1); final CyclicBarrier barrier = new CyclicBarrier(3); pinger.reset(barrier, latch, histogram); ponger.reset(barrier); executor.submit(pongProcessor); executor.submit(pingProcessor); barrier.await(); latch.await(); pingProcessor.halt(); pongProcessor.halt(); }
private void ensureTwoEventsProcessedAccordingToDependencies( final CountDownLatch countDownLatch, final DelayedEventHandler... dependencies) throws InterruptedException, BrokenBarrierException { publishEvent(); publishEvent(); for (DelayedEventHandler dependency : dependencies) { assertThatCountDownLatchEquals(countDownLatch, 2L); dependency.processEvent(); dependency.processEvent(); } assertThatCountDownLatchIsZero(countDownLatch); }
private TestEvent publishEvent() throws InterruptedException, BrokenBarrierException { if (ringBuffer == null) { ringBuffer = disruptor.start(); for (DelayedEventHandler eventHandler : delayedEventHandlers) { eventHandler.awaitStart(); } } disruptor.publishEvent( new EventTranslator<TestEvent>() { @Override public void translateTo(final TestEvent event, final long sequence) { lastPublishedEvent = event; } }); return lastPublishedEvent; }
@Override @SuppressWarnings("SleepWhileInLoop") public void run() { while (executing) { try { if (running) { long start = System.currentTimeMillis(); update(); timeLapse.getAndSet((int) (System.currentTimeMillis() - start)); } Thread.sleep(miliSeconds); if (!running || !continuous) { waiter.await(); } } catch (InterruptedException | BrokenBarrierException e) { } } }
@Test public void testJobCancelingCancelsFuture() throws InterruptedException, BrokenBarrierException { final CyclicBarrier barrier = new CyclicBarrier(2); PluggableJob<Object> job = new PluggableJob<Object>("name", new Callable<Object>() { public Object call() { try { barrier.await(); // job started: should release main thread barrier.await(); // wait for job cancel } catch (InterruptedException | BrokenBarrierException ex) { } return barrier; } }); job.schedule(); barrier.await(); job.cancel(); assertTrue("future should be cancelled by canceling()", job.getFuture().isCancelled()); barrier.await(); // job should now finish but be cancelled job.join(); assertNotNull("Job should be finished", job.getResult()); assertEquals("Should be CANCEL", IStatus.CANCEL, job.getResult().getSeverity()); }
@Test public void testFutureCancelingCancelsJob() throws InterruptedException, BrokenBarrierException { final CyclicBarrier barrier = new CyclicBarrier(2); PluggableJob<Object> job = new PluggableJob<Object>("name", new Callable<Object>() { public Object call() { try { barrier.await(); // job started: should release main thread barrier.await(); // wait for future cancel } catch (InterruptedException | BrokenBarrierException ex) { } return barrier; } }); job.schedule(); barrier.await(); // wait until job started assertEquals("Should be RUNNING", Job.RUNNING, job.getState()); job.getFuture().cancel(true); barrier.await(); // job should now finish but report as cancelled job.join(); assertNotNull("Job should be finished", job.getResult()); assertEquals("Should be CANCEL", IStatus.CANCEL, job.getResult().getSeverity()); }
@Test(groups = "standalone") public void testHEAD302() throws IOException, BrokenBarrierException, InterruptedException, ExecutionException, TimeoutException { try (AsyncHttpClient client = asyncHttpClient()) { final CountDownLatch l = new CountDownLatch(1); Request request = head("http://localhost:" + port1 + "/Test").build(); client.executeRequest(request, new AsyncCompletionHandlerBase() { @Override public Response onCompleted(Response response) throws Exception { l.countDown(); return super.onCompleted(response); } }).get(3, TimeUnit.SECONDS); if (!l.await(TIMEOUT, TimeUnit.SECONDS)) { fail("Timeout out"); } } }