try {
ThreadFactory tycThreadFactory = new ThreadFactoryBuilder().setNamePrefix("tyc-call-inf").build();
//拒绝策略,超过线程数+队列数也添加至队列,不丢弃
RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (!executor.isShutdown()) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
ThreadPoolExecutor executor = new ThreadPoolExecutor(
5,//核心线程
12,//最大线程
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1024),
tycThreadFactory,
rejectedExecutionHandler);
int taskSize=1000;
for (int i = 0; i < taskSize; i++) {
//传入一个继承Thread的线程主类
executor.execute(new ExtendsThread());
}
// 关闭线程池
executor.shutdown();
executor.awaitTermination(1000L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
标签:自定义,手动,线程,executor,catch,new,ThreadPoolExecutor
From: https://www.cnblogs.com/BoBo-B0BO/p/16919572.html