------
QQ:1755497577(备注:博客)
B站:code_ant(java相关培训视频)
demo:https://github.com/LiJinHongPassion/ThreadTest
线程池的组成一般的线程池主要分为以下 4 个组成部分:
---
观察之前提到的四种线程池的获取方法:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue());
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue()));
}
其实最终返回的都是一个ThreadPoolExecutor,那么我们只要返回一个自定义的ThreadPoolExecutor就行了
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
参数解释:
public class MyPool extends ThreadPoolExecutor {
private MyPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
/**
* 描述:自定义线程池
*
* @author LJH-1755497577 2019/10/12 14:55
* @param corePoolSize 指定了线程池中的线程数量。
* @param maximumPoolSize 指定了线程池中的最大线程数量
* @param keepAliveTime 当前线程池数量超过 corePoolSize 时,多余的空闲线程的存活时间,即多次时间内会被销毁。
* @param unit keepAliveTime 的单位。
* @param workQueue 任务队列,被提交但尚未被执行的任务。
* @param threadFactory 线程工厂,用于创建线程,一般用默认的即可。
* @param handler 拒绝策略,当任务太多来不及处理,如何拒绝任务。
* @return java.util.concurrent.ThreadPoolExecutor
*/
public static ThreadPoolExecutor getInstance(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler){
return new MyPool( corePoolSize, maximumPoolSize, keepAliveTime,
unit, workQueue, threadFactory, handler);
}
}
测试类
public class Main {
public static void main(String[] args) {
ExecutorService poolExecutor = MyPool.getInstance(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue(), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
for(int a = 0; a < 10; a ){
poolExecutor.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() "-----自定义线程池执行");
}
});
}
poolExecutor.shutdown();
}
}
线程池中的线程已经用完了,无法继续为新任务服务,同时,等待队列也已经排满了,再也塞不下新任务了。这时候我们就需要拒绝策略机制合理的处理这个问题。JDK 内置的拒绝策略如下:
注意:之前的四种拒绝策略的默认为new AbortPolicy();
阻塞队列,关键字是阻塞,先理解阻塞的含义,在阻塞队列中,线程阻塞有这样的两种情况
关于阻塞队列的详情需自行查阅百度
本文由博客一文多发平台 OpenWrite 发布!