线程池 ThreadPoolTaskExecutor

1. springboot中配置线程池

    @Bean
    public ThreadPoolTaskExecutor commonThreadPool() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(10);//核心连接数
        pool.setMaxPoolSize(50);//最大连接数
        pool.setQueueCapacity(100);//队列最大容量
        pool.setKeepAliveSeconds(3000);//存活时间
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//拒绝执行时如何处理
        return pool;
    }

2. 属性说明

maxPoolSize  最大线程数

workQueue(queueCapacity)  等待队列

  当任务到来时,线程池所有线程正忙,将任务加入到workQueue中等待,等待的任务数量不能大于 queueCapacity

corePoolSize 线程的核心数量 

  当任务到来时,若线程池中的数量还没有达到线程的核心数量,则会创建新的线程来处理任务。

  当任务到来时,若线程池中的数量大于等于核心线程数量且小于最大线程数,则只有workQueue满时才创建新的线程

  当corePoolSize 和 maxPoolSize 相等,不会再创建新的线程,若workQueue 未满,则加入workQueue中等待空闲线程处理

  当corePoolSize 和 maxPoolSize 相等且workQueue已满,则通过handler 所指定的策略来处理任务

keepAliveSeconds 存活时间

  这个时间是针对超过corePoolSize 的线程,他们执行完任务后不会立即删除,超过keepAliveSeconds 才删除

threadFactory

  用来创建线程,默认创建的线程拥有相同的优先级 且是非守护线程,同时设置线程名称

  

3. 调度执行

  3.1 execute方法

    //主线程和子线程独立,如果子线程执行时间长,主线程会先结束
    public void execute() throws Exception{
        ThreadPoolTaskExecutor threadPool = this.getThreadPool();
        for(int i = 0; i < 2; i++) {
            threadPool.execute(() -> {
                String threadName = Thread.currentThread().getName();
                try {
                    Thread.sleep(1000);
                    System.out.println(threadName);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        System.out.println("主线程结束");
    }    
    
    
    public ThreadPoolTaskExecutor getThreadPool() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.initialize();
        pool.setCorePoolSize(10);//核心连接数
        pool.setMaxPoolSize(50);//最大连接数
        pool.setQueueCapacity(15);//队列最大容量
        pool.setKeepAliveSeconds(3000);//存活时间
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//拒绝执行时如何处理
        return pool;
    }

 输出结果

主线程结束
ThreadPoolTaskExecutor-2
ThreadPoolTaskExecutor-1

  3.2 submit方法

    //submit 方法启动线程可以接收返回值,在主线程中如果不操作子线程的返回值,那么主线程和子线程是独立的
    //如果主线程操作子线程的返回值,那么主线程会等待子线程结束才结束
    public void submit() throws Exception{
        ThreadPoolTaskExecutor threadPool = this.getThreadPool();
        List<Future<String>> futures = new ArrayList<>();
        for(int i = 0; i < 2; i++) {
            Future<String> future = threadPool.submit(() -> {
                String threadName = Thread.currentThread().getName();
                try {
                    Thread.sleep(1000);
                    System.out.println(threadName);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return threadName;
            });
            futures.add(future);
        }
        
        //如果没有这个循环,主线程和主线程就是独立的
        for(Future<String> future : futures) {
            System.out.println("主线程输出:" + future.get());
        }
        System.out.println("主线程结束");        
    }
    
    public ThreadPoolTaskExecutor getThreadPool() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.initialize();
        pool.setCorePoolSize(10);//核心连接数
        pool.setMaxPoolSize(50);//最大连接数
        pool.setQueueCapacity(15);//队列最大容量
        pool.setKeepAliveSeconds(3000);//存活时间
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//拒绝执行时如何处理
        return pool;
    }

输出结果

ThreadPoolTaskExecutor-1
ThreadPoolTaskExecutor-2
主线程输出:ThreadPoolTaskExecutor-1
主线程输出:ThreadPoolTaskExecutor-2
主线程结束

原文地址:https://www.cnblogs.com/zhangzonghua/p/12878245.html