當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。