繁体   English   中英

如何在 java 播放中同时停止所有调度程序

[英]How to stop all scheudlers all at time in java play

scheduledFeeds.forEach(scheduledFeed -> {
         actorSystem.scheduler().schedule(
                Duration.create(nextExecutionTime, TimeUnit.SECONDS), // initialDelay
                Duration.create(interval, timeUnit), // interval
                () -> runJob(tenant),
                this.executionContext )

       });

我需要立即停止所有作业我需要存储所有可取消的然后取消吗?如果是,那么如何存储和取消?

当您安排任务时,将 object 存储在一个集合中,以便以后可以在需要时取消它们。

Collection<Future<?>> futures = ...;

futures.add(actorSystem.scheduler().schedule(...));

取消

futures.forEach(future -> future.cancel(true));

IIRC 可能存在需要捕获的异常。

// create a collection of cancellables

    Collection<Cancellable> futures =new ArrayList<Cancellable>();

// store the cancellables which are returned from schedulers

    futures.add(actorSystem.scheduler().schedule(...));

//then iterate each cancellable and stop the scheduler

    for(Cancellable future:futures) {
            future.cancel();
            future=null;
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM