线程池测试package cscore.concurrent.java.threadpoolv2;import java.util.concurrent.ExecutionException;import java.util.concurrent.RunnableFuture;import java.util.concurrent.TimeUnit;public class Test {  public static void main(String[] args) throws InterruptedException, ExecutionException {    var pool = new ThreadPool(2, 5, TimeUnit.SECONDS, 10, RejectPolicy.ABORT, 100000);    for (int i = 0; i < 10; i++) {      RunnableFuture<Integer> submit = pool.submit(() -> {        System.out.println(Thread.currentThread().getName() + " output a");        try {          Thread.sleep(10);        } catch (InterruptedException e) {          e.printStackTrace();        }        return 0;      });      System.out.println(submit.get());    }    int n = 15;    while (n-- > 0) {      System.out.println("Number Threads = " + pool.getCt());      Thread.sleep(1000);    }    pool.shutDown();  }}上面测试代码的输出结果如下所示:
ThreadPool-Thread-2 output aThreadPool-Thread-1 output aThreadPool-Thread-3 output aThreadPool-Thread-4 output aNumber Threads = 5ThreadPool-Thread-5 output aThreadPool-Thread-2 output aThreadPool-Thread-1 output aThreadPool-Thread-3 output aThreadPool-Thread-4 output aThreadPool-Thread-5 output aThreadPool-Thread-2 output aThreadPool-Thread-1 output aThreadPool-Thread-4 output aThreadPool-Thread-3 output aThreadPool-Thread-5 output aThreadPool-Thread-2 output aThreadPool-Thread-1 output aThreadPool-Thread-4 output aNumber Threads = 5Number Threads = 5Number Threads = 5Number Threads = 5Number Threads = 5Number Threads = 5Number Threads = 5Number Threads = 5Number Threads = 5Number Threads = 3Number Threads = 2Number Threads = 2Number Threads = 2Number Threads = 2从上面的代码可以看出我们实现了正确的任务实现结果,同时线程池当中的核心线程数从 2 变到了 5  , 当线程池当中任务队列全部别执行完成之后,线程的数目重新降下来了,这确实是我们想要达到的结果 。
总结在本篇文章当中主要给大家介绍了如何实现一个类似于JDK中的线程池,里面有非常多的实现细节,大家可以仔细捋一下其中的流程,对线程池的理解将会非常有帮助 。
推荐阅读
- 驱动开发:内核枚举进程与线程ObCall回调
 - [WPF] 抄抄超强的苹果官网滚动文字特效实现
 - 河南移动卡套餐 移动卡套餐一览表
 - reportportal 集成 robotframework 自动化执行及结果可视化
 - 驱动开发:内核枚举Registry注册表回调
 - 魔镜物语喜羊羊联动角色技能属性是什么样的
 - 微信怎么把一条消息或链接转发给自己(微信消息怎么全部转发)
 - 手机怎么自动截屏(如何停止手机自动截屏)
 - 烟雨江湖蓝鲸屿活动玩法思路是什么
 - 如何撤掉自己建的群 微信如何建立自己的微信群建群后如何撤销呢
 
