如何在 java 游戏中同时停止所有 scheudlers

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 )

       });

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

安排任务时,将该对象存储在集合中,以便以后需要时可以取消它们。

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;
                }