Java 8 CyclicBarrier - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 CyclicBarrier class.

This code example shows how to use the following methods:await
        v1.add(testStart);
        v1.add(v2);
        v2.add(testStart);
        v2.add(v1);
 
        final CyclicBarrier testEnd = new CyclicBarrier(3);
        final TestThread t1 = new TestThread(v1, testEnd);
        final TestThread t2 = new TestThread(v2, testEnd);
 
        t1.start();
        t2.start();
 
        // Wait for both test threads to have initiated serialization 
        // of the 'testStart' object (and hence of both 'v1' and 'v2') 
        testStart.await();
 
        // Wait for both test threads to successfully finish serialization 
        // of 'v1' and 'v2'. 
        System.out.println("Waiting for Vector serialization to complete ...");
        System.out.println("(This test will hang if serialization deadlocks)");

7
CodeRank
This code example shows how to use the following methods:
    } 
  } 
 
  public static void main(String[] args)
  { 
    CyclicBarrier barrier = new CyclicBarrier(3);
    new Thread(new DelayedMessagePrinter(barrier, "Enjoy")).start();
    new Thread(new DelayedMessagePrinter(barrier, "Rosetta")).start();
    new Thread(new DelayedMessagePrinter(barrier, "Code")).start();
  } 
} 
7
CodeRank
This code example shows how to use the following methods:
 */ 
@Singleton 
@Startup 
@Remote(HelperSingleton.class) 
public class HelperSingletonImpl implements HelperSingleton { 
    public static CyclicBarrier barrier = new CyclicBarrier(2);
 
    public int await(String where, long timeout, TimeUnit unit) throws BrokenBarrierException, TimeoutException, InterruptedException {
        return barrier.await(timeout, unit);
    } 
     
    public void reset() { 
        barrier.reset();
    } 
} 
7
CodeRank
This code example shows how to use the following methods:
        } 
    } 
 
    /** Test the case where the park times out. */ 
    public void test_parkFor_1() throws Exception { 
        CyclicBarrier barrier = new CyclicBarrier(2);
        Parker parker = new Parker(barrier, false, 500);
        Thread parkerThread = new Thread(parker);
        Thread waiterThread =
            new Thread(new WaitAndUnpark(barrier, 1000, parkerThread));
 
        parkerThread.start();
        waiterThread.start();
        parker.assertDurationIsInRange(500);
        waiterThread.join();
        parkerThread.join();
    } 
 
    /** Test the case where the unpark happens before the timeout. */ 
    public void test_parkFor_2() throws Exception { 
6
CodeRank
This code example shows how to use the following methods:await
 
      AssertJUnit.assertEquals((Integer) 1, future.get());
   } 
 
   public void testTaskCancellation() throws Exception {
      CyclicBarrier barrier = new CyclicBarrier(2);
      cache(1, cacheName()).getAdvancedCache().getComponentRegistry().registerComponent(barrier, "barrier");
 
      DistributedExecutorService des = createDES(getCache());
      List<Address> cacheMembers = getCache().getAdvancedCache().getRpcManager().getMembers();
      AssertJUnit.assertEquals(caches(cacheName()).size(), cacheMembers.size());
 
      DistributedTaskBuilder<Integer> tb = des.createDistributedTaskBuilder(new LongRunningCallable());
      final Future<Integer> future = des.submit(address(1), tb.build());
      // Will unblock when LongRunningCallable starts running 
      barrier.await(10, TimeUnit.SECONDS);
      future.cancel(true);
      boolean taskCancelled = false;
      try { 
         future.get();

6
CodeRank
This code example shows how to use the following methods:await
 
        // Populate the hashtables so that they refer to each other 
        h1.put(testStart, h2);
        h2.put(testStart, h1);
 
        final CyclicBarrier testEnd = new CyclicBarrier(3);
        final TestThread t1 = new TestThread(h1, testEnd);
        final TestThread t2 = new TestThread(h2, testEnd);
 
        t1.start();
        t2.start();
 
        // Wait for both test threads to have initiated serialization 
        // of the 'testStart' object (and hence of both 'h1' and 'h2') 
        testStart.await();
 
        // Wait for both test threads to successfully finish serialization 
        // of 'h1' and 'h2'. 
        System.out.println("Waiting for Hashtable serialization to complete ...");
        System.out.println("(This test will hang if serialization deadlocks)");

6
CodeRank
This code example shows how to use the following methods:
   public void testMixed(Method m) throws InterruptedException {
      runTest(StresserThread.MIXED_OPS_PERFORMER, cache, cache, keys, getThreadName(m));
   } 
 
   static void runTest(StresserThread.OperationsPerformer ops, Cache c1, Cache c2, List<Integer> keys, String threadNamePrefix) throws InterruptedException {
      CyclicBarrier beforeCommit = new CyclicBarrier(2);
 
      StresserThread st1 = new StresserThread(c1, keys, "t1", ops, beforeCommit, threadNamePrefix + "-1");
      final ArrayList<Integer> reversedKeys = new ArrayList<Integer>(keys);
      Collections.reverse(reversedKeys);
      StresserThread st2 = new StresserThread(c2, reversedKeys, "t2", ops, beforeCommit, threadNamePrefix+"-2");
 
      st1.start();
      st2.start();
 
      st1.join();
      st2.join();
 
      assert !st1.isError();
      assert !st2.isError();
6
CodeRank
This code example shows how to use the following methods:await
 
        // Populate the hashtables so that they refer to each other 
        h1.put(testStart, h2);
        h2.put(testStart, h1);
 
        final CyclicBarrier testEnd = new CyclicBarrier(3);
        final TestThread t1 = new TestThread(h1, testEnd);
        final TestThread t2 = new TestThread(h2, testEnd);
 
        t1.start();
        t2.start();
 
        // Wait for both test threads to have initiated serialization 
        // of the 'testStart' object (and hence of both 'h1' and 'h2') 
        testStart.await();
 
        // Wait for both test threads to successfully finish serialization 
        // of 'h1' and 'h2'. 
        System.out.println("Waiting for Hashtable serialization to complete ...");
        System.out.println("(This test will hang if serialization deadlocks)");

6
CodeRank
This code example shows how to use the following methods:await
 
	private static final int SUB_THREAD_SIZE = 3;
	 
	public static void runWork() { 
		LogUtils.LOGGER.info("Start!"); 
		CyclicBarrier barrier = new CyclicBarrier(SUB_THREAD_SIZE + 1);    // +1 ,让主线程等待所有子线程结束
		Thread thread = null; 
		SingersExtCalculator cSingersExt = new SingersExtCalculator(barrier);
		AlbumsExtCalculator  cAlbumsExt  = new AlbumsExtCalculator(barrier);
		SongsExtCalculator   cSongsExt   = new SongsExtCalculator(barrier);
		thread = new Thread(cSingersExt);
		thread.start();
		thread = new Thread(cAlbumsExt);
		thread.start();
		thread = new Thread(cSongsExt);
		thread.start();
		mainAwait(barrier);
		LogUtils.LOGGER.info("Finish!"); 
	} 
	 

6
CodeRank
This code example shows how to use the following methods:await
    final String layout1 = "layout-id-1";
    final String layout2 = "layout-id-2";
 
    ZooKeeperUtils.setTableLayout(mZKClient, tableURI, layout1);
 
    final CyclicBarrier barrier = new CyclicBarrier(2);
    TableLayoutTracker tracker = new TableLayoutTracker(mZKClient, tableURI,
            new BarrierTableLayoutUpdateHandler(barrier));
    try { 
      tracker.start();
 
      barrier.await(5, TimeUnit.SECONDS);
      Assert.assertEquals(layout1, tracker.getLayoutID());
 
      ZooKeeperUtils.setTableLayout(mZKClient, tableURI, layout2);
      barrier.await(5, TimeUnit.SECONDS);
      Assert.assertEquals(layout2, tracker.getLayoutID());
    } finally { 
      tracker.close();
    } 
  } 
6
CodeRank

Related Java 8 CyclicBarrier Questions & Answers:

See Code Examples for Java 8 CyclicBarrier Methods: