springboot 使用线程池,异步执行方法

一、线程池配置类 

package com.lh.spring.boot.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description: 线程池配置
 * @author: lianghao
 * @create: 12/9/2019 10:15 AM
 * EnableAsync : 开启异步调用
 **/
@Configuration
@EnableAsync
public class ThreadExecutorConfig {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /** 核心线程数 */
    private int corePoolSize = 10;
    /** 最大线程数 */
    private int maxPoolSize = 200;
    /** 队列数 */
    private int queueCapacity = 10;


    @Bean
    public ExecutorService calcThreadExecutor(){
        logger.info("start build threadPoolTaskExecutor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("calc-thread");

        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 执行初始化
        executor.initialize();
        return executor.getThreadPoolExecutor();
    }

}

 二、其他代码

 

package com.lh.spring.boot.thread;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @description:
 * @author: lianghao
 * @create: 12/9/2019 4:16 PM
 **/
@SpringBootApplication
public class ThreadApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(ThreadApplication.class);
        springApplication.run(args);
    }
}
package com.lh.spring.boot.thread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: lianghao
 * @create: 12/9/2019 4:20 PM
 **/
@RestController
public class ThreadController {

    @Autowired
    private ThreadService testService;

    @RequestMapping("/test")
    public String test() throws InterruptedException {
        testService.executeAsync();
        return "34567";
    }
}
package com.lh.spring.boot.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: lianghao
 * @create: 12/9/2019 4:17 PM
 **/
@Service
public class ThreadService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Async("calcThreadExecutor")
    public void executeAsync() throws InterruptedException {

        logger.info("start executeAsync");
        Thread.sleep(1000);
        logger.info("end executeAsync");
    }
}

启动ThreadApplication ,在浏览器中输入http://localhost:8080/test ,可以看到数据是直接返回的,并没有等到一秒。

springboot 使用线程池,异步执行方法_第1张图片

springboot 使用线程池,异步执行方法_第2张图片 

你可能感兴趣的:(Java技术)