类java.util.concurrent.BrokenBarrierException源码实例Demo

下面列出了怎么用java.util.concurrent.BrokenBarrierException的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void shouldGenerateUniqueIdsInMultithreadedEnvironment()
    throws BrokenBarrierException, InterruptedException {
  AwsXRayIdsGenerator generator = new AwsXRayIdsGenerator();
  Set<TraceId> traceIds = new CopyOnWriteArraySet<>();
  Set<SpanId> spanIds = new CopyOnWriteArraySet<>();
  int threads = 8;
  int generations = 128;
  CyclicBarrier barrier = new CyclicBarrier(threads + 1);
  Executor executor = Executors.newFixedThreadPool(threads);
  for (int i = 0; i < threads; i++) {
    executor.execute(new GenerateRunner(generations, generator, barrier, traceIds, spanIds));
  }
  barrier.await();
  barrier.await();
  assertThat(traceIds).hasSize(threads * generations);
  assertThat(spanIds).hasSize(threads * generations);
}
 
源代码2 项目: mzmine3   文件: ScoreAligner.java
@Override
public void run() {
  while (!aligningDone()) {
    align();
    // Exceptions cause wrong results but do not report
    // that in any way
    try {
      barrier.await();
    } catch (InterruptedException e) {
      return;
    } catch (BrokenBarrierException e2) {
      return;
    } catch (CancellationException e3) {
      return;
    }
  }
}
 
源代码3 项目: dragonwell8_jdk   文件: B4769350.java
@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);
    }
}
 
源代码4 项目: TencentKona-8   文件: BusyLock.java
@Override
public void run() {
    try {
        // wait until forceAbort leave monitor
        barrier.await();
        if (UNSAFE.tryMonitorEnter(monitor)) {
            try {
                barrier.await();
                Thread.sleep(timeout);
            } finally {
                UNSAFE.monitorExit(monitor);
            }
        } else {
            throw new RuntimeException("Monitor should be entered by " +
                                       "::run() first.");
        }
    } catch (InterruptedException | BrokenBarrierException e) {
        throw new RuntimeException("Synchronization error happened.", e);
    }
}
 
源代码5 项目: jdk8u-jdk   文件: B4769350.java
@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);
            }
}
 
源代码6 项目: TencentKona-8   文件: B4769350.java
@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);
            }
}
 
源代码7 项目: TencentKona-8   文件: B4769350.java
@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);
    }
}
 
源代码8 项目: java-concurrency-patterns   文件: UsingBarriers.java
public static void main(String[] args) {

		Runnable barrierAction = () -> System.out.println("Well done, guys!");

		var executor = Executors.newCachedThreadPool();
		var barrier = new CyclicBarrier(10, barrierAction);

		Runnable task = () -> {
			try {
				// simulating a task that can take at most 1sec to run
				System.out.println("Doing task for " + Thread.currentThread().getName());
				Thread.sleep(new Random().nextInt(10) * 100);
				System.out.println("Done for " + Thread.currentThread().getName());
				barrier.await();
			} catch (InterruptedException | BrokenBarrierException e) {
				e.printStackTrace();
			}
		};

		for (int i = 0; i < 10; i++) {
			executor.execute(task);
		}
		executor.shutdown();

	}
 
源代码9 项目: blog-sample   文件: CyclicBarrierDemo.java
public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();
    CyclicBarrier cyclicBarrier = new CyclicBarrier(6);
    for (int i = 1; i <= 18; i++) {
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "开始等待其他线程");
            try {
                cyclicBarrier.await();
                System.out.println(Thread.currentThread().getName() + "开始执行业务逻辑,耗时0.5秒");
                // 工作线程开始处理,这里用Thread.sleep()来模拟业务处理
                Thread.sleep(500);
                System.out.println(Thread.currentThread().getName() + "业务逻辑执行完毕");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        });
    }
    executorService.shutdown();
}
 
源代码10 项目: apm-agent-java   文件: HelloServer.java
private void syncWait(boolean isStart) {
    Sync sync = syncBarriers.get();
    if (sync != null) {
        String step = isStart ? "start" : "end";
        logVerbose("server waiting sync on " + step);
        CyclicBarrier barrier = isStart ? sync.processingStart : sync.processingEnd;
        long waitStart = System.currentTimeMillis();
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            throw new RuntimeException(e);
        } finally {
            barrier.reset();
        }
        long waitedMillis = System.currentTimeMillis() - waitStart;
        logVerbose("waited for {} ms at processing {}", waitedMillis, step);
    }
}
 
@Test
public void subscribeCloseSynchronously() throws Exception {
    AtomicReference<Future<?>> futureRef = new AtomicReference<>();
    toSource(cbos.connect().afterOnSubscribe(subscription -> {
        // We want to increase the chance that the writer thread has to wait for the Subscriber to become
        // available, instead of waiting for the requestN demand.
        CyclicBarrier barrier = new CyclicBarrier(2);
        futureRef.compareAndSet(null, executorService.submit(toRunnable(() -> {
            barrier.await();
            cbos.close();
        })));
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            throw new RuntimeException(e);
        }
    })).subscribe(subscriber);

    Future<?> f = futureRef.get();
    assertNotNull(f);
    f.get();
    assertThat(subscriber.takeTerminal(), is(complete()));
}
 
源代码12 项目: servicetalk   文件: ConnectablePayloadWriterTest.java
@Test
public void subscribeCloseSynchronously() throws Exception {
    AtomicReference<Future<?>> futureRef = new AtomicReference<>();
    toSource(cpw.connect().afterOnSubscribe(subscription -> {
        // We want to increase the chance that the writer thread has to wait for the Subscriber to become
        // available, instead of waiting for the requestN demand.
        CyclicBarrier barrier = new CyclicBarrier(2);
        futureRef.compareAndSet(null, executorService.submit(toRunnable(() -> {
            barrier.await();
            cpw.close();
        })));
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            throw new RuntimeException(e);
        }
    })).subscribe(subscriber);

    Future<?> f = futureRef.get();
    assertNotNull(f);
    f.get();
    assertThat(subscriber.takeTerminal(), is(complete()));
}
 
源代码13 项目: jdk8u60   文件: BusyLock.java
@Override
public void run() {
    try {
        // wait until forceAbort leave monitor
        barrier.await();
        if (UNSAFE.tryMonitorEnter(monitor)) {
            try {
                barrier.await();
                Thread.sleep(timeout);
            } finally {
                UNSAFE.monitorExit(monitor);
            }
        } else {
            throw new RuntimeException("Monitor should be entered by " +
                                       "::run() first.");
        }
    } catch (InterruptedException | BrokenBarrierException e) {
        throw new RuntimeException("Synchronization error happened.", e);
    }
}
 
源代码14 项目: jdk8u60   文件: B4769350.java
@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);
            }
}
 
源代码15 项目: jdk8u60   文件: B4769350.java
@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);
    }
}
 
源代码16 项目: jdk8u60   文件: B4769350.java
@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");
    }
}
 
源代码17 项目: jdk8u60   文件: B4769350.java
@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");
    }
}
 
源代码18 项目: Oak   文件: WorkloadMemoryTest.java
@Override
public void run() {
    try {
        barrier.await();
    } catch (InterruptedException | BrokenBarrierException e) {
        e.printStackTrace();
    }

    Random r = new Random();
    while (!stop.get()) {
        Integer key = r.nextInt(NUM_OF_ENTRIES);
        int op = r.nextInt(100);

        if (op < getPercents) {
            oak.zc().get(key);
        } else {
            oak.zc().put(key, 8);
        }
    }
}
 
源代码19 项目: openjdk-jdk8u   文件: BusyLock.java
@Override
public void run() {
    try {
        // wait until forceAbort leave monitor
        barrier.await();
        if (UNSAFE.tryMonitorEnter(monitor)) {
            try {
                barrier.await();
                Thread.sleep(timeout);
            } finally {
                UNSAFE.monitorExit(monitor);
            }
        } else {
            throw new RuntimeException("Monitor should be entered by " +
                                       "::run() first.");
        }
    } catch (InterruptedException | BrokenBarrierException e) {
        throw new RuntimeException("Synchronization error happened.", e);
    }
}
 
源代码20 项目: openjdk-jdk8u   文件: B4769350.java
@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);
            }
}
 
源代码21 项目: openjdk-jdk8u   文件: B4769350.java
@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);
    }
}
 
源代码22 项目: javacore   文件: CyclicBarrierDemo02.java
@Override
public void run() {
    try {
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + " waiting at barrier 1");
        this.barrier1.await();

        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + " waiting at barrier 2");
        this.barrier2.await();

        System.out.println(Thread.currentThread().getName() + " done!");
    } catch (InterruptedException | BrokenBarrierException e) {
        e.printStackTrace();
    }
}
 
源代码23 项目: openjdk-jdk9   文件: CyclicBarrierTest.java
/**
 * 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);
}
 
源代码24 项目: openjdk-jdk9   文件: CyclicBarrierTest.java
/**
 * 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);
}
 
源代码25 项目: openjdk-jdk8u-backup   文件: B4769350.java
@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);
            }
}
 
源代码26 项目: openjdk-jdk9   文件: CyclicBarrierTest.java
/**
 * 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);
}
 
源代码27 项目: cyberduck   文件: BackgroundActionPauser.java
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);
    }
}
 
源代码28 项目: cyberduck   文件: TransferQueueTest.java
@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());
}
 
源代码29 项目: swim   文件: MockClock.java
@Override
protected void sleep(long millis) throws InterruptedException {
  try {
    this.tickBarrier.await();
    this.tickBarrier.await();
  } catch (BrokenBarrierException cause) {
    throw new RuntimeException(cause);
  }
}
 
源代码30 项目: swim   文件: MockClock.java
void halfTick() {
  try {
    tickBarrier.await();
  } catch (BrokenBarrierException | InterruptedException error) {
    throw new TestException(error);
  }
}
 
 类所在包
 同包方法