gpt4 book ai didi

java - ThreadPoolTask​​Scheduler 如何异步每个 future

转载 作者:行者123 更新时间:2023-12-02 01:43:51 25 4
gpt4 key购买 nike

Autowiring 线程池任务计划:

@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(10);
return threadPoolTaskScheduler;
}

future 的测试:

public void test(){
ScheduledFuture future = threadPoolTaskScheduler.schedule(new MyFuture(), new CronTrigger("0/5 * * * * ?"));
}
private class MyFuture implements Runnable{
@Override
public void run() {
int a=0;
for(int i=0;i<1000000;i++){
for(int j=0;j<1000000;j++){
a++;
}
}
}
}

当我运行test()时,我发现第一个线程正在运行,但时间超过5秒,第二个线程没有运行并等待第一个线程完成。现在如何让线程异步执行并且不等待最后一个线程完成? enter image description here

enter image description here

最佳答案

CronTrigger 的 JavaDoc 内容如下

/**
* Determine the next execution time according to the given trigger context.
* <p>Next execution times are calculated based on the
* {@linkplain TriggerContext#lastCompletionTime completion time} of the
* previous execution; therefore, overlapping executions won't occur.
*/

因此因此,不会发生重叠执行。如果您确实需要重叠任务,则需要采取一些解决方法。

关于java - ThreadPoolTask​​Scheduler 如何异步每个 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54000881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com