小编Qua*_*ien的帖子

Spring Boot 中@Async、@Scheduled 和线程池的正确使用

我写了很长时间的问题,很长。但我试图尽可能多地展示我做了什么,什么是不清楚的。请完成阅读并感谢您的耐心等待!

我尝试了很多实验,写了 spring doc spring doc,(在本站写问题)但还是没看懂全貌。

我有一项任务是在一个 spring-boot 服务器中实现一些调度程序。

  1. First Scheduler 将每 1 秒检查一次 DB 中的数据并运行一些逻辑。
  2. Second Scheduler 将每 10 毫秒向第三方服务发送一次请求。

South 调度程序必须与线程池一起使用并具有不同的设置。例如第一个 - 5 个线程,第二个 - 10 个线程。虽然我明白了,但我尝试了几个选项,最后还是糊涂了,我应该选择什么以及如何更正确地使用它:

为了测试,我创建了 2 个带有逻辑的 bean,并且每次都会从这个 bean 调用方法:

@Slf4j
@Component
public class TestBean {

    public void test(){
        try {
            Thread.sleep(9000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("First bean print");
    }
}
Run Code Online (Sandbox Code Playgroud)

@Slf4j
@Component
public class TestBean2 {

    public void test(){
        try {
            Thread.sleep(9000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } …
Run Code Online (Sandbox Code Playgroud)

java spring asynchronous scheduled-tasks threadpool

6
推荐指数
1
解决办法
3393
查看次数

标签 统计

asynchronous ×1

java ×1

scheduled-tasks ×1

spring ×1

threadpool ×1