线程池的创建方式:

可以使用jdk原生Executors创建,可缓存,可定时,指定大小,以及单例。

   public static void main(String[] args) {
       //可缓存
       ExecutorService executorService = Executors.newCachedThreadPool();
       executorService.execute(new Runnable() {
           @Override
           public void run() {
           }
       });
       //指定大小-可定长度
       ExecutorService executorService1 = Executors.newFixedThreadPool(10);
       executorService1.execute(new Runnable() {
           @Override
           public void run() {

           }
       });
       //单例
       ExecutorService executorService2 = Executors.newSingleThreadExecutor();
       //可定时
       ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(109);
   }

手写线程池

/**
 * 创建一个固定的线程-一直在运行
 */
public class MyExecutors {
    private List<WorkThread> workThreadList;
    private  LinkedBlockingDeque linkedBlockingDeque;
    private boolean isRunning = true;

    /**
     *
     * @param maxThreadCount 最多多个线程
     * @param deque  做多缓存多少
     */
    public MyExecutors(int maxThreadCount,int deque){
        linkedBlockingDeque=new LinkedBlockingDeque(deque);
        workThreadList=new ArrayList<WorkThread>();

        for (int i = 0; i < maxThreadCount; i++) {
        new WorkThread().start();
        }

    }
    public boolean execute(Runnable command){
        boolean offer = linkedBlockingDeque.offer(command);
        return offer;
    }
    class WorkThread extends Thread {
        @Override
        public void run() {
            while (isRunning|| linkedBlockingDeque.size()>0) {
                Runnable poll = (Runnable) linkedBlockingDeque.poll();
                if (poll != null) {
                    poll.run();
                }
            }
        }
    }
}

ThreadPoolExecutor核心参数

jdk自带的线程池都是基于ThreadPoolExecutor

核心线程数----正在运行的线程

最大线程数

x销毁时间(超过核心线程数 多久销毁)

最大线程数-核心线程数【最多同时运行的线程数】

线程池创建的线程式一直都会运行么

不会。