Pause and resume a ScheduledExecutorService

主宰稳场 提交于 2019-12-24 04:27:45

问题


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

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?

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/27750459/pause-and-resume-a-scheduledexecutorservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!