栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java并发编程系列23:Executors框架和ThreadPoolExecutor实现类了解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java并发编程系列23:Executors框架和ThreadPoolExecutor实现类了解

Executors框架成员:

ThreadPoolExecutor实现类、ScheduledThreadPoolExecutor实现类、Future接口、Runnable接口、Callable接口和Executors。

java.util.concurrent.Executor : 负责线程的使用与调度的根接口 
 |–ExecutorService:Executor的子接口,线程池的主要接口 
   |–ThreadPoolExecutor:ExecutorService的实现类 
   |–ScheduledExecutorService:ExecutorService的子接口,负责线程的调度 
     |–ScheduledThreadPoolExecutor:既继承了ThreadPoolExecutor,同时实现了ScheduledExecutorService

ThreadPoolExecutor实现类了解:

构造方法:

    public ThreadPoolExecutor(int corePoolSize,

                              int maximumPoolSize,

                              long keepAliveTime,

                              TimeUnit unit,

                              BlockingQueue workQueue) {

        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,

             Executors.defaultThreadFactory(), defaultHandler);

    }    

public ThreadPoolExecutor(int corePoolSize,

                              int maximumPoolSize,

                              long keepAliveTime,

                              TimeUnit unit,

                              BlockingQueue workQueue,

                              ThreadFactory threadFactory) {

        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,

             threadFactory, defaultHandler);

    }

参数介绍:

  1. int corePoolSize, 核心线程大小
  2. int maximumPoolSize,最大线程大小
  3. long keepAliveTime, 超过corePoolSize的线程多久不活动被销毁时间
  4. TimeUnit unit,时间单位
  5. BlockingQueue workQueue 任务队列
  6. ThreadFactory threadFactory 线程池工厂
  7. RejectedExecutionHandler handler 拒绝策略

以下介绍三种类型的ThreadPoolExecutor:SingleThreadExecutor、FixedThreadPool和CachedThreadPool。

这3种线程池都是创建了ThreadPollExecutor对象,只是传递的参数不一样,传入的workQueue 都是默认,即最大可添加Integer.MAX_VALUE个任务,这也就是阿里巴巴java开发规范禁止直接使用java提供的默认线程池的原因。

参考:https://blog.csdn.net/weixin_41888813/article/details/90769126

1、FixedThreadPool

线程数量固定的线程池(定长线程池),可控制线程最大并发数,超出的线程会在队列中等待。

它适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。

    public static ExecutorService newFixedThreadPool(int nThreads) {

        return new ThreadPoolExecutor(nThreads, nThreads,

                                      0L, TimeUnit.MILLISECONDS,

                                      new linkedBlockingQueue());

}

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {

        return new ThreadPoolExecutor(nThreads, nThreads,

                                      0L, TimeUnit.MILLISECONDS,

                                      new linkedBlockingQueue(),

                                      threadFactory);

}

  1. 初始线程数为:nThreads
  2. 最大线程数为:nThreads
  3. 超时时间为:0毫秒
  4. 阻塞队列采用的是:linkedBlockingQueue

线程池最大线程数量maxmumPoolSize和核心线程池的数量corePoolSize设置为相等,使用LinkedBlockingQueue作为阻塞队列。

2、SingleThreadExecutor

只有一个线程的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

它适用于需要保证顺序地执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。

    public static ExecutorService newSingleThreadExecutor() {

        return new FinalizableDelegatedExecutorService

            (new ThreadPoolExecutor(1, 1,

                                    0L, TimeUnit.MILLISECONDS,

                                    new linkedBlockingQueue()));

}

    public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {

        return new FinalizableDelegatedExecutorService

            (new ThreadPoolExecutor(1, 1,

                                    0L, TimeUnit.MILLISECONDS,

                                    new linkedBlockingQueue(),

                                    threadFactory));

    }

  1. 初始线程数为:1
  2. 最大线程数为:1
  3. 超时时间为:0毫秒
  4. 阻塞队列采用的是:linkedBlockingQueue

3、newCachedThreadPool

一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

它适用于执行很多的短期异步任务的小程序,或者是负载较轻的服务器。

    public static ExecutorService newCachedThreadPool() {

        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

                                      60L, TimeUnit.SECONDS,

                                      new SynchronousQueue());

}

    public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {

        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

                                      60L, TimeUnit.SECONDS,

                                      new SynchronousQueue(),

                                      threadFactory);

    }

  1. 初始线程数为:0
  2. 最大线程数为: Integer.MAX_VALUE(int的最大值)
  3. 超时时间为:1分钟
  4. 阻塞队列采用的是:SynchronousQueue

参考:https://www.cnblogs.com/xiondun/p/14764096.html

参考:https://blog.csdn.net/tongdanping/article/details/79625109

参考:https://blog.csdn.net/qq_43040688/category_9910156.html

参考:https://blog.csdn.net/qq_43040688/article/details/106046629

参考:关于阿里规范禁止使用Executors创建线程池的分析

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732609.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号