本文整理匯總了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());
}
}
示例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());
}
}
示例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));
}
示例4: awaitPoolSize
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
static void awaitPoolSize(ThreadPoolExecutor pool, int n) {
while (pool.getPoolSize() != n) Thread.yield();
pass();
}