ThreadPoolExecutor的七个参数:

  • corePoolSize 核心线程数
  • MaxPS  最大线程数
  • keepAliveTime  存活时间
  • TimeUnit  时间单位
  • BlockingQueue  任务队列
  • ThreadFactory  线程工厂
  • RejectStrategy  拒绝策略

Abort    抛异常

Discard 悄悄扔掉

DiscardOld 扔掉最先的任务

CallerRuns 谁提交任务谁来执行

package com.zhangxueliang.demo.springbootdemo.JUC.c_026_01_ThreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName springbootdemo_src
 * @ClassName T05_00_HelloThreadPool
 * @Desicription TODO
 * @Author Zhang Xueliang
 * @Date 2019/12/5 15:34
 * @Version 1.0
 **/
public class T05_00_HelloThreadPool{


    public class Task implements Runnable{
        int i;

        public Task(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            System.err.println(Thread.currentThread().getName()+" "+i);
        }
    }
}

class Test_T05_00_HelloThreadPool{
    public static void main(String[] args) {
        T05_00_HelloThreadPool t05_00_helloThreadPool = new T05_00_HelloThreadPool();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        try{
            for (int i = 0; i < 8; i++) {
                threadPoolExecutor.execute(t05_00_helloThreadPool.new Task(i));
            }
            System.err.println(threadPoolExecutor.getQueue());
        }finally {
            threadPoolExecutor.shutdown();
        }
    }
}

ThreadPoolExecutor的七个参数详解_后台编程