本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.setRejectedExecutionHandler方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.setRejectedExecutionHandler方法的具体用法?Java ThreadPoolExecutor.setRejectedExecutionHandler怎么用?Java ThreadPoolExecutor.setRejectedExecutionHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.setRejectedExecutionHandler方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(2));
executor.setRejectedExecutionHandler((task, exec) -> System.out.println(task.toString() + " : Rejected"));
// Add some tasks
for (int i = 0; i < 9; i++) {
executor.execute(new WorkerTask("Task " + (i + 1)));
// Sleep
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
executor.shutdown();
executor.awaitTermination(20, TimeUnit.SECONDS);
}
示例2: ReadaheadPool
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private ReadaheadPool() {
pool = new ThreadPoolExecutor(POOL_SIZE, MAX_POOL_SIZE, 3L, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(CAPACITY));
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
pool.setThreadFactory(new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("Readahead Thread #%d")
.build());
}
示例3: Server
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* Constructor of the class. Creates the executor object
*/
public Server(){
// Create the executor
executor=(ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Create the controller for the Rejected tasks
RejectedTaskController controller=new RejectedTaskController();
// Establish the rejected task controller
executor.setRejectedExecutionHandler(controller);
}
示例4: testSetRejectedExecutionHandler
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* setRejectedExecutionHandler sets the handler returned by
* getRejectedExecutionHandler
*/
public void testSetRejectedExecutionHandler() {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
RejectedExecutionHandler handler = new NoOpREHandler();
p.setRejectedExecutionHandler(handler);
assertSame(handler, p.getRejectedExecutionHandler());
}
}
示例5: testSetRejectedExecutionHandlerNull
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* setRejectedExecutionHandler(null) throws NPE
*/
public void testSetRejectedExecutionHandlerNull() {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
try {
p.setRejectedExecutionHandler(null);
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例6: testSetRejectedExecutionHandler
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* setRejectedExecutionHandler sets the handler returned by
* getRejectedExecutionHandler
*/
public void testSetRejectedExecutionHandler() {
final ThreadPoolExecutor p =
new CustomTPE(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
RejectedExecutionHandler handler = new NoOpREHandler();
p.setRejectedExecutionHandler(handler);
assertSame(handler, p.getRejectedExecutionHandler());
}
}
示例7: testSetRejectedExecutionHandlerNull
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* setRejectedExecutionHandler(null) throws NPE
*/
public void testSetRejectedExecutionHandlerNull() {
final ThreadPoolExecutor p =
new CustomTPE(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
try {
p.setRejectedExecutionHandler(null);
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例8: setRejectedExecutionHandler
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
void setRejectedExecutionHandler(
ThreadPoolExecutor p, RejectedExecutionHandler handler) {
p.setRejectedExecutionHandler(handler);
assertSame(handler, p.getRejectedExecutionHandler());
}