Java Code Examples for java.util.concurrent.ScheduledThreadPoolExecutor#awaitTermination()

The following examples show how to use java.util.concurrent.ScheduledThreadPoolExecutor#awaitTermination() . 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: ThreadRestarts.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void test(boolean allowTimeout) throws Exception {
    CountingThreadFactory ctf = new CountingThreadFactory();
    ScheduledThreadPoolExecutor stpe
        = new ScheduledThreadPoolExecutor(10, ctf);
    try {
        // schedule a dummy task in the "far future"
        Runnable nop = new Runnable() { public void run() {}};
        stpe.schedule(nop, FAR_FUTURE_MS, MILLISECONDS);
        stpe.setKeepAliveTime(1L, MILLISECONDS);
        stpe.allowCoreThreadTimeOut(allowTimeout);
        MILLISECONDS.sleep(12L);
    } finally {
        stpe.shutdownNow();
        if (!stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
            throw new AssertionError("timed out");
    }
    if (ctf.count.get() > 1)
        throw new AssertionError(
            String.format("%d threads created, 1 expected",
                          ctf.count.get()));
}
 
Example 2
Source File: CoreClientTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testCoreClientWithInjectedThreadPools() throws Exception {

   ExecutorService threadPool = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
   ScheduledThreadPoolExecutor scheduledThreadPool = new ScheduledThreadPoolExecutor(10);

   ServerLocator locator = createNonHALocator(false);
   boolean setThreadPools = locator.setThreadPools(threadPool, scheduledThreadPool);

   assertTrue(setThreadPools);
   testCoreClient(true, locator);

   threadPool.shutdown();
   scheduledThreadPool.shutdown();

   threadPool.awaitTermination(60, TimeUnit.SECONDS);
   scheduledThreadPool.awaitTermination(60, TimeUnit.SECONDS);
}
 
Example 3
Source File: Scheduler.java    From crate with Apache License 2.0 5 votes vote down vote up
static boolean awaitTermination(final ScheduledThreadPoolExecutor scheduledThreadPoolExecutor,
        final long timeout, final TimeUnit timeUnit) {
    try {
        if (scheduledThreadPoolExecutor.awaitTermination(timeout, timeUnit)) {
            return true;
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return false;
}
 
Example 4
Source File: Stress.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Throwable {

        final CountDownLatch count = new CountDownLatch(1000);

        final ScheduledThreadPoolExecutor pool =
            new ScheduledThreadPoolExecutor(100);
        pool.prestartAllCoreThreads();

        final Runnable incTask = new Runnable() { public void run() {
            count.countDown();
        }};

        pool.scheduleAtFixedRate(incTask, 0, 10, TimeUnit.MILLISECONDS);

        count.await();

        pool.shutdown();
        pool.awaitTermination(1L, TimeUnit.DAYS);
    }