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 ,可以看到数据是直接返回的,并没有等到一秒。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值