美文网首页
ThreadPoolExecutor七大构造参数详解

ThreadPoolExecutor七大构造参数详解

作者: yincb | 来源:发表于2019-05-06 00:51 被阅读0次
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)
  • corePoolSize:线程池核心线程数(平时保留的线程数)

  • maximumPoolSize:线程池最大线程数(当workQueue都放不下时,启动新线程,最大线程数)

  • keepAliveTime:超出corePoolSize数量的线程的保留时间。

  • unit:keepAliveTime单位

  • workQueue:阻塞队列,存放来不及执行的线程
    ArrayBlockingQueue:构造函数一定要传大小
    LinkedBlockingQueue:构造函数不传大小会默认为(Integer.MAX_VALUE ),当大量请求任务时,容易造成 内存耗尽。
    SynchronousQueue:同步队列,一个没有存储空间的阻塞队列 ,将任务同步交付给工作线程。
    PriorityBlockingQueue : 优先队列

  • threadFactory:线程工厂

  • handler:饱和策略
    AbortPolicy(默认):直接抛弃
    CallerRunsPolicy:用调用者的线程执行任务
    DiscardOldestPolicy:抛弃队列中最久的任务
    DiscardPolicy:抛弃当前任务

example

public class ThreadPoolExecutorBuild {

    public static void main(String[] args) {
        ThreadPoolExecutor executorService = (ThreadPoolExecutor)buildThreadPoolExecutor();

        int activeCount = -1;
        int queueSize = -1;
        while (true){
            if(activeCount != executorService.getActiveCount() || queueSize != executorService.getQueue().size()){
                System.out.println("ActiveCount= "+executorService.getActiveCount());
                System.out.println("CorePoolSize= "+executorService.getCorePoolSize());
                System.out.println("QueueSize= "+executorService.getQueue().size());
                System.out.println("MaximumPoolSize= "+executorService.getMaximumPoolSize());
                activeCount = executorService.getActiveCount();
                queueSize = executorService.getQueue().size();
                System.out.println("================================");
            }
        }
    }
    private static ExecutorService buildThreadPoolExecutor(){
        ExecutorService executorService= new ThreadPoolExecutor(
                1,
                2,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1),
                r -> {
                    Thread t = new Thread(r);
                    return t;
                    },
                new ThreadPoolExecutor.AbortPolicy());
        System.out.println("the ThreadPoolExecutor create done.");

        executorService.execute(()->sleepSeconds(100));
        executorService.execute(()->sleepSeconds(10));
        executorService.execute(()->sleepSeconds(10));

        return executorService;
    }

    private static void sleepSeconds(int seconds){
        try {
            System.out.println("[*"+Thread.currentThread().getName()+"*]");
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

运行结果:
the ThreadPoolExecutor create done.
[Thread-0]
[Thread-1]
ActiveCount= 2
CorePoolSize= 1
QueueSize= 1
MaximumPoolSize= 2
================================
[Thread-1]
ActiveCount= 2
CorePoolSize= 1
QueueSize= 0
MaximumPoolSize= 2
================================
ActiveCount= 1
CorePoolSize= 1
QueueSize= 0
MaximumPoolSize= 2
================================
ActiveCount= 0
CorePoolSize= 1
QueueSize= 0
MaximumPoolSize= 2
================================

相关文章

网友评论

      本文标题:ThreadPoolExecutor七大构造参数详解

      本文链接:https://www.haomeiwen.com/subject/bohloqtx.html