当前位置: 首页> 娱乐> 影视 > 上海环球金融中心观光厅_网站设计步骤图片_seo策划_网站运营策划书范文

上海环球金融中心观光厅_网站设计步骤图片_seo策划_网站运营策划书范文

时间:2025/7/13 23:12:49来源:https://blog.csdn.net/qq_42192693/article/details/142497164 浏览次数:0次
上海环球金融中心观光厅_网站设计步骤图片_seo策划_网站运营策划书范文

【问题】执行下面代码后,关闭ThreadPoolTaskScheduler,CronTask仍然继续执行。

@Configuration
public class config {@Beanpublic String getString() throws InterruptedException {Runnable runnable = () -> {try {System.out.println("hello runnable");Thread.sleep(2000);} catch (Throwable e) {e.printStackTrace();}};CronTask cronTask = new CronTask(() -> {try {System.out.println("hello cronTask");Thread.sleep(2000);} catch (Throwable e) {e.printStackTrace();}}, "* * * * * ?");ThreadPoolTaskScheduler testScheduler = new ThreadPoolTaskScheduler();testScheduler.setPoolSize(2);testScheduler.initialize();testScheduler.scheduleWithFixedDelay(runnable, 10);testScheduler.schedule(cronTask.getRunnable(), cronTask.getTrigger());Thread.sleep(6000);testScheduler.setAwaitTerminationMillis(100);testScheduler.setWaitForTasksToCompleteOnShutdown(true);testScheduler.shutdown();System.out.println("shutdown");return new String("123");}
}
=================================================
hello runnable
hello cronTask
hello runnable
hello cronTask
hello runnable
shutdown
hello cronTask

schedule(CronTask task):使用 CronTask 允许你基于 Cron 表达式定义复杂的调度规则。任务将在指定的时间点按 Cron 表达式执行,例如每天的特定时间或每小时。
scheduleWithFixedDelay(Runnable task, long delay):这个方法基于固定的延迟时间调度任务。任务在上一次执行完成后,等待指定的延迟时间,然后再次执行。适合需要间隔性执行的任务,不关心具体的执行时间。

【解决办法】

setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean executeExistingDelayedTasks)
  • 如果设置为 true,在调用 shutdown 方法后,已经排定但尚未执行的延迟任务仍然会被执行。
  • 如果设置为 false,则在 shutdown 后,所有已排定的延迟任务将不会被执行。
testScheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
关键字:上海环球金融中心观光厅_网站设计步骤图片_seo策划_网站运营策划书范文

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: