本文整理匯總了Java中java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy方法的典型用法代碼示例。如果您正苦於以下問題:Java ThreadPoolExecutor.DiscardOldestPolicy方法的具體用法?Java ThreadPoolExecutor.DiscardOldestPolicy怎麽用?Java ThreadPoolExecutor.DiscardOldestPolicy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.DiscardOldestPolicy方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testSaturatedExecute_DiscardOldestPolicy
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* executor using DiscardOldestPolicy drops oldest task if saturated.
*/
public void testSaturatedExecute_DiscardOldestPolicy() {
final CountDownLatch done = new CountDownLatch(1);
LatchAwaiter r1 = awaiter(done);
LatchAwaiter r2 = awaiter(done);
LatchAwaiter r3 = awaiter(done);
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(LatchAwaiter.NEW, r1.state);
assertEquals(LatchAwaiter.NEW, r2.state);
assertEquals(LatchAwaiter.NEW, r3.state);
p.execute(r1);
p.execute(r2);
assertTrue(p.getQueue().contains(r2));
p.execute(r3);
assertFalse(p.getQueue().contains(r2));
assertTrue(p.getQueue().contains(r3));
}
assertEquals(LatchAwaiter.DONE, r1.state);
assertEquals(LatchAwaiter.NEW, r2.state);
assertEquals(LatchAwaiter.DONE, r3.state);
}
示例2: testDiscardOldestOnShutdown
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* execute using DiscardOldestPolicy drops task on shutdown
*/
public void testDiscardOldestOnShutdown() {
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
}
}
示例3: TransferModule
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
private TransferModule(){
mThreadPool = new ThreadPoolExecutor(10, 20, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
}