當前位置: 首頁>>代碼示例>>Java>>正文


Java ThreadPoolExecutor.getPoolSize方法代碼示例

本文整理匯總了Java中java.util.concurrent.ThreadPoolExecutor.getPoolSize方法的典型用法代碼示例。如果您正苦於以下問題:Java ThreadPoolExecutor.getPoolSize方法的具體用法?Java ThreadPoolExecutor.getPoolSize怎麽用?Java ThreadPoolExecutor.getPoolSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.ThreadPoolExecutor的用法示例。


在下文中一共展示了ThreadPoolExecutor.getPoolSize方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testAllowCoreThreadTimeOut_true

import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
 * allowCoreThreadTimeOut(true) causes idle threads to time out
 */
public void testAllowCoreThreadTimeOut_true() throws Exception {
    long keepAliveTime = timeoutMillis();
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(2, 10,
                               keepAliveTime, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        p.allowCoreThreadTimeOut(true);
        p.execute(new CheckedRunnable() {
            public void realRun() {
                threadStarted.countDown();
                assertEquals(1, p.getPoolSize());
            }});
        await(threadStarted);
        delay(keepAliveTime);
        long startTime = System.nanoTime();
        while (p.getPoolSize() > 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        assertEquals(0, p.getPoolSize());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,代碼來源:ThreadPoolExecutorTest.java

示例2: testAllowCoreThreadTimeOut_true

import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
 * allowCoreThreadTimeOut(true) causes idle threads to time out
 */
public void testAllowCoreThreadTimeOut_true() throws Exception {
    long keepAliveTime = timeoutMillis();
    final ThreadPoolExecutor p =
        new CustomTPE(2, 10,
                      keepAliveTime, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        p.allowCoreThreadTimeOut(true);
        p.execute(new CheckedRunnable() {
            public void realRun() {
                threadStarted.countDown();
                assertEquals(1, p.getPoolSize());
            }});
        await(threadStarted);
        delay(keepAliveTime);
        long startTime = System.nanoTime();
        while (p.getPoolSize() > 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        assertEquals(0, p.getPoolSize());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,代碼來源:ThreadPoolExecutorSubclassTest.java

示例3: realMain

import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
private static void realMain(String[] args) throws Throwable {
    final int n = 4;
    final CyclicBarrier barrier = new CyclicBarrier(2*n+1);
    final ThreadPoolExecutor pool
        = new ThreadPoolExecutor(n, 2*n,
                                 KEEPALIVE_MS, MILLISECONDS,
                                 new SynchronousQueue<Runnable>());
    final Runnable r = new Runnable() { public void run() {
        try {
            barrier.await();
            barrier.await();
        } catch (Throwable t) { unexpected(t); }}};

    for (int i = 0; i < 2*n; i++)
        pool.execute(r);
    barrier.await();
    checkPoolSizes(pool, 2*n, n, 2*n);
    barrier.await();
    long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
    for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
        check((sleepyTime += nap) <= LONG_DELAY_MS);
        Thread.sleep(nap);
    }
    checkPoolSizes(pool, n, n, 2*n);
    Thread.sleep(nap);
    checkPoolSizes(pool, n, n, 2*n);
    pool.shutdown();
    check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:TimeOutShrink.java

示例4: awaitPoolSize

import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
static void awaitPoolSize(ThreadPoolExecutor pool, int n) {
    while (pool.getPoolSize() != n) Thread.yield();
    pass();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:5,代碼來源:ModifyCorePoolSize.java


注:本文中的java.util.concurrent.ThreadPoolExecutor.getPoolSize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。