暂停并恢复ScheduledExecutorService [英] Pause and resume a ScheduledExecutorService

查看:432
本文介绍了暂停并恢复ScheduledExecutorService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个俄罗斯方块.我希望每60秒使碎片落下一点点,为此,我正在使用

I'm writing a tetris-clone. I want to make the pieces fall a tad faster after every 60 seconds, and for this I'm using a ScheduledExecutorService:

executor.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        levelUp();
    }
}, 60, 60, TimeUnit.SECONDS);

现在,我可以暂停游戏了,而暂停时,游戏显然会停止执行任何操作,直到我继续游戏为止.就像现在这样,暂停游戏是一件好事,除了这位执行者负责升级"之外,所有事情都可以暂停.例如,如果我玩了50秒钟,暂停了游戏一分钟,然后又继续玩了10秒钟,我将处于第2级,而我应该处于第1级(我们从第0级开始).有没有一种方法可以暂停恢复执行器?

Now, I can pause the game, and while paused the game obviously stops doing anything until I resume it. As it is now, pausing the game is well and all for everything but this executor taking care of "leveling up". For example, if I played for 50 seconds, paused the game for a minute, resumed and played another 10 seconds, I would be at level 2, while I'm supposed to be at level 1 (we start at level 0). Is there a way of pausing and resuming the executor?

请注意,仅在暂停时不进行升级是不够的:如果我播放50秒,然后暂停20秒,然后继续播放,那么在升级之前,我还必须再播放50秒,而不是原来的10秒

Note that simply not leveling up while paused is not enough: if I play for 50 seconds, then pause for 20 seconds and then resume, I would have to play another 50 seconds before I level up, instead of the supposed 10 seconds.

那么,有没有办法暂停和恢复ScheduledExecutorService?

So, is there a way to pause and resume a ScheduledExecutorService?

推荐答案

查看问题.默认情况下,不支持暂停和恢复ScheduledExecutorService,可以将其停止.您必须处理任务的安排.

Look at this and this questions. By default there is no support to pause and resume a ScheduledExecutorService, it just can be stopped. You have to play with the scheduling of the tasks.

这篇关于暂停并恢复ScheduledExecutorService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆