当前位置: 首页>>代码示例>>Java>>正文


Java ScheduledThreadPoolExecutor.shutdownNow方法代码示例

本文整理汇总了Java中java.util.concurrent.ScheduledThreadPoolExecutor.shutdownNow方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledThreadPoolExecutor.shutdownNow方法的具体用法?Java ScheduledThreadPoolExecutor.shutdownNow怎么用?Java ScheduledThreadPoolExecutor.shutdownNow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ScheduledThreadPoolExecutor的用法示例。


在下文中一共展示了ScheduledThreadPoolExecutor.shutdownNow方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
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()));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:ThreadRestarts.java

示例2: testShutdownNow

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * shutdownNow returns a list containing tasks that were not run,
 * and those tasks are drained from the queue
 */
public void testShutdownNow() throws InterruptedException {
    final int poolSize = 2;
    final int count = 5;
    final AtomicInteger ran = new AtomicInteger(0);
    final ScheduledThreadPoolExecutor p =
        new ScheduledThreadPoolExecutor(poolSize);
    final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
    Runnable waiter = new CheckedRunnable() { public void realRun() {
        threadsStarted.countDown();
        try {
            MILLISECONDS.sleep(2 * LONG_DELAY_MS);
        } catch (InterruptedException success) {}
        ran.getAndIncrement();
    }};
    for (int i = 0; i < count; i++)
        p.execute(waiter);
    await(threadsStarted);
    assertEquals(poolSize, p.getActiveCount());
    assertEquals(0, p.getCompletedTaskCount());
    final List<Runnable> queuedTasks;
    try {
        queuedTasks = p.shutdownNow();
    } catch (SecurityException ok) {
        return; // Allowed in case test doesn't have privs
    }
    assertTrue(p.isShutdown());
    assertTrue(p.getQueue().isEmpty());
    assertEquals(count - poolSize, queuedTasks.size());
    assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
    assertTrue(p.isTerminated());
    assertEquals(poolSize, ran.get());
    assertEquals(poolSize, p.getCompletedTaskCount());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:ScheduledExecutorTest.java

示例3: testShutdownNow_delayedTasks

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * shutdownNow returns a list containing tasks that were not run,
 * and those tasks are drained from the queue
 */
public void testShutdownNow_delayedTasks() throws InterruptedException {
    final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    List<ScheduledFuture> tasks = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        Runnable r = new NoOpRunnable();
        tasks.add(p.schedule(r, 9, SECONDS));
        tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
        tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
    }
    if (testImplementationDetails)
        assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
    final List<Runnable> queuedTasks;
    try {
        queuedTasks = p.shutdownNow();
    } catch (SecurityException ok) {
        return; // Allowed in case test doesn't have privs
    }
    assertTrue(p.isShutdown());
    assertTrue(p.getQueue().isEmpty());
    if (testImplementationDetails)
        assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
    assertEquals(tasks.size(), queuedTasks.size());
    for (ScheduledFuture task : tasks) {
        assertFalse(task.isDone());
        assertFalse(task.isCancelled());
    }
    assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
    assertTrue(p.isTerminated());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ScheduledExecutorTest.java

示例4: realMain

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
static void realMain(String[] args) throws Throwable {
    final int size = 10;
    final ScheduledThreadPoolExecutor pool
        = new ScheduledThreadPoolExecutor(size);
    final Runnable nop = new Runnable() { public void run() {}};

    for (int i = 0; i < size; i++)
        pool.scheduleAtFixedRate(nop, 100L * (i + 1),
                                 1000L, TimeUnit.MILLISECONDS);
    awaitPoolSize(pool, size);
    setCorePoolSize(pool, size - 3);
    setCorePoolSize(pool, size + 3);
    pool.shutdownNow();
    check(pool.awaitTermination(1L, TimeUnit.DAYS));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:ModifyCorePoolSize.java

示例5: test

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void test(String[] args) throws Throwable {
    ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(0);
    try {
        test(p);
    } finally {
        p.shutdownNow();
        check(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ZeroCoreThreads.java


注:本文中的java.util.concurrent.ScheduledThreadPoolExecutor.shutdownNow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。