Olavi Mustanoja
Olavi Mustanoja

Reputation: 2065

Pause and resume a ScheduledExecutorService

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?

Upvotes: 2

Views: 9626

Answers (1)

marczeeee
marczeeee

Reputation: 181

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.

Upvotes: 2

Related Questions