springboot启用线程池

1、在application.properties中配置线程池
springboot启用线程池

server.port=8088

#线程池配置
task.queue.corePoolSize=10
task.queue.maxPoolSize=30
task.queue.queueCapacity=8
task.queue.keepAlive=60

baseurl=http://localhost:8080
#login
login_url=/sys/login/restful.do
logout_url=/logout
updateUserSessionByusername_url=/updateUserSessionByusername.do
syslogin_url=/sys/login.do

shuiyin_url=/yisheng/getUserWaterMark.do
currentpermission_url=/permissions/current.do

#kaosheng
kaosheng_infoByCode_url=/kaosheng/infoByCode
kaosheng_updateFenzuByKsIds=/kaosheng/updateFenzuByKsIds
kaosheng_zuChengYuanByZuCode_url=/access/kaosheng/zuChengYuanByZuCode

#fenzu
fenzu_getZuList_url=/fenzu/getZuList.do

#xuexiao
xuexiao_getShengShiQuXueXiao_url=/xuexiao/getShengShiQuXueXiao

#user
updatePass_url=/users/updatePass.do

2、创建线程池配置类:

package com.zr.gktjweb.aspect;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置、启用异步
 *
 * @author liugh
 */
//开启异步
@EnableAsync(proxyTargetClass = true)
@Configuration
public class AsycTaskExecutorConfig {

    @Value("${task.queue.corePoolSize}")
    private int corePoolSize;//线程池维护线程的最少数量
    @Value("${task.queue.maxPoolSize}")
    private int maxPoolSize;//线程池维护线程的最大数量
    @Value("${task.queue.queueCapacity}")
    private int queueCapacity; //缓存队列
    @Value("${task.queue.keepAlive}")
    private int keepAlive;//允许的空闲时间

    @Bean
    public Executor taskQueueExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(corePoolSize);
        // 设置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        // 设置队列容量
        executor.setQueueCapacity(queueCapacity);
        // 设置默认线程名称
        executor.setThreadNamePrefix("taskQueueExecutor-");
        //对拒绝task的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 设置线程活跃时间(秒)
        executor.setKeepAliveSeconds(keepAlive);
        executor.initialize();
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}

3、添加线程异步:参照https://blog.****.net/aiyongbo123456/article/details/86635455

4、访问controller层,结果如下:
springboot启用线程池
成功调用线程池中创建的线程。