ThreadPoolTask Scheduler不适用于线程池

问题描述 投票:0回答:2

我创造

@Bean
ThreadPoolTaskScheduler taskScheduler(){
    ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(5);
    threadPoolTaskScheduler.setAwaitTerminationSeconds(1);
    threadPoolTaskScheduler.setThreadNamePrefix("Test-");
    threadPoolTaskScheduler.initialize();
    return threadPoolTaskScheduler;
}

在我的组件中我使用它:

@PostConstruct
    public void test() {
        taskScheduler.scheduleWithFixedDelay(() -> {
            try {
                Thread.sleep(9000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("test");
        }, 1000L);
    }

我等待每1秒钟从PoolSize(5)开始一个线程,并且在5个时间后池将满,我将等待第一个免费线程并且它继续工作。

但实际上我看到下一个:

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 - 结果是一样的

Aaditi:

@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 {

没有帮助:

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
java spring multithreading scheduled-tasks scheduler
2个回答
1
投票

如果你想要执行任务,你需要使它成为Async,即使有一个正在运行,例如:

@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");
}

0
投票

感谢提示@Sun我找到了解决方案:

@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(testBean::test, 1000L);
}

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

@Slf4j
@Component
public class TestBean {

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

并将@EnableAsync放在我的配置类上

© www.soinside.com 2019 - 2024. All rights reserved.