Java Code Examples for java.util.concurrent.ForkJoinPool#awaitQuiescence()

The following examples show how to use java.util.concurrent.ForkJoinPool#awaitQuiescence() . 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: DatastoreBrowseNodeStoreImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testDeleteNodeConcurrency() throws Exception {
  populateFullRepository();

  ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());

  pool.submit(() -> generatedAssets().forEach(asset -> pool.submit(() -> delete(asset))));
  pool.submit(() -> generatedComponents().forEach(component -> pool.submit(() -> delete(component))));

  pool.awaitQuiescence(10, TimeUnit.SECONDS);

  assertRepositoryEmpty(
      generatedRepositories()
          .stream()
          .filter(repo -> repo.contentRepositoryId() == 1)
          .findFirst()
          .orElseThrow(() -> new NoSuchElementException("Repository with id 1 does not exist"))
          .contentRepositoryId()
  );
}
 
Example 2
Source File: ForkJoinService.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
public void handle(Message message) {
    SpanContext spanCtx = getTracer().extract(Format.Builtin.TEXT_MAP,
            new TextMapExtractAdapter(message.getHeaders()));

    // Top level, so create Tracer and root span
    Span serverSpan = getTracer().buildSpan("Server")
            .asChildOf(spanCtx)
            .withTag(Constants.ZIPKIN_BIN_ANNOTATION_HTTP_URL, "http://localhost:8080/inbound?orderId=123&verbose=true")
            .withTag("orderId", "1243343456455")
            .start();

    delay(500);

    ForkJoinPool pool = new ForkJoinPool();
    for (int i = 0; i < 5; i++) {
        int pos = i;
        pool.execute(() -> component(serverSpan, pos));
    }

    pool.awaitQuiescence(5, TimeUnit.SECONDS);

    serverSpan.finish();

    serverSpan.close();
}
 
Example 3
Source File: MappedFaweQueue.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void optimize() {
    final ForkJoinPool pool = TaskManager.IMP.getPublicForkJoinPool();
    map.forEachChunk(new RunnableVal<FaweChunk>() {
        @Override
        public void run(final FaweChunk chunk) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    chunk.optimize();
                }
            });
        }
    });
    pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
 
Example 4
Source File: RetryingHelloWorldClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  boolean enableRetries = !Boolean.parseBoolean(System.getenv(ENV_DISABLE_RETRYING));
  final RetryingHelloWorldClient client = new RetryingHelloWorldClient("localhost", 50051, enableRetries);
  ForkJoinPool executor = new ForkJoinPool();

  for (int i = 0; i < 50; i++) {
    final String userId = "user" + i;
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            client.greet(userId);
          }
        });
  }
  executor.awaitQuiescence(100, TimeUnit.SECONDS);
  executor.shutdown();
  client.printSummary();
  client.shutdown();
}
 
Example 5
Source File: HedgingHelloWorldClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  boolean hedging = !Boolean.parseBoolean(System.getenv(ENV_DISABLE_HEDGING));
  final HedgingHelloWorldClient client = new HedgingHelloWorldClient("localhost", 50051, hedging);
  ForkJoinPool executor = new ForkJoinPool();

  for (int i = 0; i < 2000; i++) {
    final String userId = "user" + i;
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            client.greet(userId);
          }
        });
  }

  executor.awaitQuiescence(100, TimeUnit.SECONDS);
  executor.shutdown();
  client.printSummary();
  client.shutdown();
}
 
Example 6
Source File: ForkJoinPool8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * awaitQuiescence by a worker is equivalent in effect to
 * ForkJoinTask.helpQuiesce()
 */
public void testAwaitQuiescence1() throws Exception {
    final ForkJoinPool p = new ForkJoinPool();
    try (PoolCleaner cleaner = cleaner(p)) {
        final long startTime = System.nanoTime();
        assertTrue(p.isQuiescent());
        ForkJoinTask a = new CheckedRecursiveAction() {
            protected void realCompute() {
                FibAction f = new FibAction(8);
                assertSame(f, f.fork());
                assertSame(p, ForkJoinTask.getPool());
                boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
                assertTrue(quiescent);
                assertFalse(p.isQuiescent());
                while (!f.isDone()) {
                    assertFalse(p.getAsyncMode());
                    assertFalse(p.isShutdown());
                    assertFalse(p.isTerminating());
                    assertFalse(p.isTerminated());
                    Thread.yield();
                }
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
                assertFalse(p.isQuiescent());
                assertEquals(0, ForkJoinTask.getQueuedTaskCount());
                assertEquals(21, f.result);
            }};
        p.execute(a);
        while (!a.isDone() || !p.isQuiescent()) {
            assertFalse(p.getAsyncMode());
            assertFalse(p.isShutdown());
            assertFalse(p.isTerminating());
            assertFalse(p.isTerminated());
            Thread.yield();
        }
        assertEquals(0, p.getQueuedTaskCount());
        assertFalse(p.getAsyncMode());
        assertEquals(0, p.getQueuedSubmissionCount());
        assertFalse(p.hasQueuedSubmissions());
        while (p.getActiveThreadCount() != 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertFalse(p.isShutdown());
        assertFalse(p.isTerminating());
        assertFalse(p.isTerminated());
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
Example 7
Source File: ForkJoinPool8Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * awaitQuiescence by a worker is equivalent in effect to
 * ForkJoinTask.helpQuiesce()
 */
public void testAwaitQuiescence1() throws Exception {
    final ForkJoinPool p = new ForkJoinPool();
    try (PoolCleaner cleaner = cleaner(p)) {
        final long startTime = System.nanoTime();
        assertTrue(p.isQuiescent());
        ForkJoinTask a = new CheckedRecursiveAction() {
            protected void realCompute() {
                FibAction f = new FibAction(8);
                assertSame(f, f.fork());
                assertSame(p, ForkJoinTask.getPool());
                boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
                assertTrue(quiescent);
                assertFalse(p.isQuiescent());
                while (!f.isDone()) {
                    assertFalse(p.getAsyncMode());
                    assertFalse(p.isShutdown());
                    assertFalse(p.isTerminating());
                    assertFalse(p.isTerminated());
                    Thread.yield();
                }
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
                assertFalse(p.isQuiescent());
                assertEquals(0, ForkJoinTask.getQueuedTaskCount());
                assertEquals(21, f.result);
            }};
        p.execute(a);
        while (!a.isDone() || !p.isQuiescent()) {
            assertFalse(p.getAsyncMode());
            assertFalse(p.isShutdown());
            assertFalse(p.isTerminating());
            assertFalse(p.isTerminated());
            Thread.yield();
        }
        assertEquals(0, p.getQueuedTaskCount());
        assertFalse(p.getAsyncMode());
        assertEquals(0, p.getQueuedSubmissionCount());
        assertFalse(p.hasQueuedSubmissions());
        while (p.getActiveThreadCount() != 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertFalse(p.isShutdown());
        assertFalse(p.isTerminating());
        assertFalse(p.isTerminated());
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}