欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

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

时间:2023-07-03
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);

    }

参数介绍:

int corePoolSize, 核心线程大小int maximumPoolSize,最大线程大小long keepAliveTime, 超过corePoolSize的线程多久不活动被销毁时间TimeUnit unit,时间单位BlockingQueue workQueue 任务队列ThreadFactory threadFactory 线程池工厂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);

}

初始线程数为:nThreads最大线程数为:nThreads超时时间为:0毫秒阻塞队列采用的是: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超时时间为:0毫秒阻塞队列采用的是: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);

    }

初始线程数为:0最大线程数为: Integer.MAX_VALUE(int的最大值)超时时间为:1分钟阻塞队列采用的是: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创建线程池的分析

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。