• 47.各种类型的线程池


    线程池继承体系

    Executor(interface)->ExecutorService(interface)->ThreadPoolExecutor(class)

    Executors.newFixedThreadPool

     核心线程数=最大线程数(没有救急线程被创建),所以也无需超时时间
     阻塞队列LinkedBlockingQueue,可以放任意数量的任务,队列容量=Integer.MAX_VALUE
     适用于:任务量已知,相对耗时的任务
    

    1. public static void main(String[] args) {
    2. ExecutorService executorService = Executors.newFixedThreadPool(2);
    3. executorService.execute(() -> {
    4. log.debug("1");
    5. });
    6. executorService.execute(() -> {
    7. log.debug("2");
    8. });
    9. executorService.execute(() -> {
    10. log.debug("3");
    11. });
    12. }

    ThreadFactory自定义线程名称

    1. public static void main(String[] args) {
    2. ExecutorService executorService = Executors.newFixedThreadPool(2,
    3. new ThreadFactory() {
    4. private final AtomicInteger threadNumber = new AtomicInteger(1);
    5. private final String namePrefix = "xkj_";
    6. @Override
    7. public Thread newThread(Runnable r) {
    8. Thread t = new Thread(r,
    9. namePrefix + threadNumber.getAndIncrement());
    10. return t;
    11. }
    12. });
    13. executorService.execute(() -> {
    14. log.debug("1");
    15. });
    16. executorService.execute(() -> {
    17. log.debug("2");
    18. });
    19. executorService.execute(() -> {
    20. log.debug("3");
    21. });
    22. }

     

    Executors.newCachedThreadPool()

     没有核心线程,全是救急线程,线程存活时间是1分钟,救急线程可以无限创建Integer.MAX_VALUE。

    SynchronousQueue 同步队列,特点是,没有容量,没有线程来取是放不进去的(一手交钱,一手交货)

    1. ExecutorService executorService = Executors.newCachedThreadPool();
    2. executorService.execute(() -> {
    3. log.debug("111");
    4. });
    5. executorService.execute(() -> {
    6. log.debug("222");
    7. });
    8. executorService.execute(() -> {
    9. log.debug("333");
    10. });

    SynchronousQueue同步队列

    一手交钱一手交货,没有线程来取任务,就无法添加新任务。

    1. SynchronousQueue synchronousQueue = new SynchronousQueue<>();
    2. new Thread(() -> {
    3. try {
    4. log.debug("putting{}", 1);
    5. synchronousQueue.put(1);//会阻塞住
    6. log.debug("putted{}", 1);
    7. log.debug("putting{}", 2);
    8. synchronousQueue.put(2);//会阻塞住
    9. log.debug("putted{}", 2);
    10. } catch (InterruptedException e) {
    11. e.printStackTrace();
    12. }
    13. }, "t1").start();
    14. new Thread(() -> {
    15. try {
    16. Integer take = synchronousQueue.take();//取出元素会让put方法后面继续执行,不再阻塞
    17. log.debug("take:{}", take);
    18. }catch (InterruptedException e) {
    19. e.printStackTrace();
    20. }
    21. }, "t2").start();

    适用于:任务数比较密集,但每个任务执行时间较短的情况。

    Executors.newSingleThreadExecutor

    希望多个任务排队执行,线程数固定为1。

    任务数多于1时,会放入无界队列排队。

    任务执行完毕,这唯一的线程也不会被释放。

    跟自定义创建一个单线程串行执行任务的区别:

    如果任务执行失败而终止没有任何补救措施,而线程池还会新建一个线程,保证池的正常工作。

    FinalizableDelegatedExecutorService应用的是装饰器模式,只是对外暴露了ExecutorService接口,因此不能调用ThreadPoolExecutor中特有的方法。

    Executors.newFixedThreadPool(1)初始时为1,以后还可以修改。对外暴露的是ThreadPoolExecutor对象,可以强转后调用setCorePoolSize等方法进行修改。

    1. ExecutorService executorService = Executors.newSingleThreadExecutor();
    2. executorService.execute(() -> {
    3. log.debug("aaa");
    4. int i = 1 / 0;
    5. });
    6. executorService.execute(() -> {
    7. log.debug("bbb");
    8. });
    9. executorService.execute(() -> {
    10. log.debug("ccc");
    11. });

  • 相关阅读:
    GEE15:获取不同遥感指数的时间序列及不同指数间的关系
    基于springboot的疫情防控管理系统
    设计模式-享元模式
    go基础10 -字符串的高效构造与转换
    Spring注解-3.自动装配
    电脑翻译软件-在线电脑实时翻译软件
    (算法设计与分析)第三章动态规划-第二节:动态规划之背包类型问题
    【Spring Cloud系统】- Zookeer特性与使用场景
    使用DIV+CSS技术设计的非遗文化网页与实现制作(web前端网页制作课作业)
    安科瑞变电站综合自动化系统在青岛海洋科技园应用
  • 原文地址:https://blog.csdn.net/qq_36352889/article/details/139453068