首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ThreadPoolTaskScheduler不使用线程池

ThreadPoolTaskScheduler不使用线程池
EN

Stack Overflow用户
提问于 2018-09-04 15:08:43
回答 2查看 1.2K关注 0票数 0

我创造

代码语言:javascript
复制
@Bean
ThreadPoolTaskScheduler taskScheduler(){
    ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(5);
    threadPoolTaskScheduler.setAwaitTerminationSeconds(1);
    threadPoolTaskScheduler.setThreadNamePrefix("Test-");
    threadPoolTaskScheduler.initialize();
    return threadPoolTaskScheduler;
}

在我的组件中,我使用它:

代码语言:javascript
复制
@PostConstruct
    public void test() {
        taskScheduler.scheduleWithFixedDelay(() -> {
            try {
                Thread.sleep(9000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("test");
        }, 1000L);
    }

我等待每1秒将从PoolSize(5)启动一个线程,5Tims池将满,我将等待第一个空闲线程,它将继续工作。

但在现实中我看到了下一个:

代码语言:javascript
复制
2018-09-04 18:06:42.769  INFO 10128 --- [main] c.e.scheduling.SchedulingApplication : Started SchedulingApplication in 1.69 seconds (JVM running for 2.193)
2018-09-04 18:06:51.385  INFO 10128 --- [Test-1] com.example.scheduling.MyScheduler : test
2018-09-04 18:07:01.387  INFO 10128 --- [Test-1] com.example.scheduling.MyScheduler : test
2018-09-04 18:07:11.389  INFO 10128 --- [Test-2] com.example.scheduling.MyScheduler : test

每9秒进行一次线程打印测试

编辑

我测试了

scheduleAtFixedRate --结果是相同的

EDIT2

代码语言:javascript
复制
@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(this::test2, 1000L);
}

@Async
public void test2() {
    try {
        Thread.sleep(9000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.info("test");
}

@EnableAsync
@EnableScheduling
@Configuration
public class JavaConfig {

无济于事:

代码语言:javascript
复制
2018-09-05 10:31:40.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:31:49.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:31:58.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:07.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:16.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:25.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:34.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:43.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:52.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:33:01.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:33:10.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:33:19.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
EN

回答 2

Stack Overflow用户

发布于 2018-09-04 15:31:28

如果您想要执行任务,即使有一个正在运行,则需要将其设置为Async,例如:

代码语言:javascript
复制
@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(this::makeLog, 1000);
}

@Async
public void makeLog() {
    try {
        Thread.sleep(9000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.info("test");
}
票数 1
EN

Stack Overflow用户

发布于 2018-09-05 07:54:39

由于提示@Sun,我找到了解决方案:

代码语言:javascript
复制
@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(testBean::test, 1000L);
}

并将方法移动到另一个类,因为默认情况下我使用proxy

代码语言:javascript
复制
@Slf4j
@Component
public class TestBean {

    @Async
    public void test(){
        try {
            Thread.sleep(9000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("hz");
    }
}

并将@EnableAsync放到我的配置类中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52169310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档