Executor框架最核心的类是ThreadPoolExecutor,它是线程池的实现类,主要由下列4个组件构成。 |
---|
·corePool:核心线程池的大小。 |
·maximumPool:最大线程池的大小。 |
·BlockingQueue:用来暂时保存任务的工作队列。 |
·RejectedExecutionHandler:当ThreadPoolExecutor已经关闭或ThreadPoolExecutor已经饱和时(达到了最大线程池大小且工作队列已满),execute()方法将要调用的Handler。 |
-------------------------------------------------------------------------读书笔记摘自 书名:Java并发编程的艺术 作者:方腾飞;魏鹏;程晓明 |
线程任务
public class Task implements Runnable {
private final String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " → " + name + " Start Time = " + new Date());
processCommand();
System.out.println(Thread.currentThread().getName() + " → " + name + " End Time = " + new Date());
}
private void processCommand() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
自定义线程池
public class CustomThreadPool {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + "线程: Start at: " + new Date());
// 创建等待队列
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(20);
// 创建线程池、池中保存的线程数为3,允许的最大线程数为5
ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 5, 50, TimeUnit.MILLISECONDS, blockingQueue);
// 创建七个任务
// 每个任务会在一个线程上执行
for (int i = 1; i < 10; i++) {
System.out.println("添加了第" + i + "个任务类");
pool.execute(new Task("线程任务" + i));
}
// 关闭线程池
pool.shutdown();
System.out.println(Thread.currentThread().getName() + "线程: 打卡:" + new Date());
long i = 0;
while (!pool.isTerminated()) {
// wait for all tasks to finish
i++;
}
System.out.println(Thread.currentThread().getName() + "线程: Finished all threads at:" + new Date() + ". isTerminated 判断次数 " + i);
}
}
执行结果分析
执行结果 | |
---|---|
main线程: Start at: Fri May 19 18:28:52 CST 2023 | 进入main 线程 |
添加了第1个任务类 | 构建任务放入线程池
|
添加了第2个任务类 | |
添加了第3个任务类 | |
添加了第4个任务类 | |
添加了第5个任务类 | |
添加了第6个任务类 | |
添加了第7个任务类 | |
添加了第8个任务类 | |
添加了第9个任务类 | |
添加了第10个任务类 | |
main线程: 打卡:Fri May 19 18:28:52 CST 2023 | 主线程打个卡 |
pool-1-thread-2 → 线程任务2 Start Time = Fri May 19 18:28:52 CST 2023 |
线程池 开始分配线程执行线程任务 |
pool-1-thread-1 → 线程任务1 Start Time = Fri May 19 18:28:52 CST 2023 | |
pool-1-thread-3 → 线程任务3 Start Time = Fri May 19 18:28:52 CST 2023 | |
pool-1-thread-2 → 线程任务2 End Time = Fri May 19 18:28:55 CST 2023 | |
pool-1-thread-2 → 线程任务4 Start Time = Fri May 19 18:28:55 CST 2023 | |
pool-1-thread-3 → 线程任务3 End Time = Fri May 19 18:28:55 CST 2023 | |
pool-1-thread-1 → 线程任务1 End Time = Fri May 19 18:28:55 CST 2023 | |
pool-1-thread-1 → 线程任务6 Start Time = Fri May 19 18:28:55 CST 2023 | |
pool-1-thread-3 → 线程任务5 Start Time = Fri May 19 18:28:55 CST 2023 | |
pool-1-thread-2 → 线程任务4 End Time = Fri May 19 18:28:58 CST 2023 | |
pool-1-thread-2 → 线程任务7 Start Time = Fri May 19 18:28:58 CST 2023 | |
pool-1-thread-3 → 线程任务5 End Time = Fri May 19 18:28:58 CST 2023 | |
pool-1-thread-1 → 线程任务6 End Time = Fri May 19 18:28:58 CST 2023 | |
pool-1-thread-3 → 线程任务8 Start Time = Fri May 19 18:28:58 CST 2023 | |
pool-1-thread-1 → 线程任务9 Start Time = Fri May 19 18:28:58 CST 2023 | |
pool-1-thread-2 → 线程任务7 End Time = Fri May 19 18:29:01 CST 2023 | |
pool-1-thread-2 → 线程任务10 Start Time = Fri May 19 18:29:01 CST 2023 | |
pool-1-thread-3 → 线程任务8 End Time = Fri May 19 18:29:01 CST 2023 | |
pool-1-thread-1 → 线程任务9 End Time = Fri May 19 18:29:01 CST 2023 | |
pool-1-thread-2 → 线程任务10 End Time = Fri May 19 18:29:04 CST 2023 | |
main线程: Finished all threads at:Fri May 19 18:29:04 CST 2023. isTerminated 判断次数 25692088316 | 所有线程全部执行完毕 |
从结果中可以看出,10 个任务是在线程池的 3 个线程上执行的 |
文章来源地址https://www.toymoban.com/news/detail-453297.html
文章来源:https://www.toymoban.com/news/detail-453297.html
到了这里,关于自定义线程池 ThreadPoolExecutor的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!