Scenario:
I am making a clone of a popular card game. There needs to be a period during which people can join the game. Sometimes, the period is too short. Therefore, I want to let people extend the waiting period.
For the example numbers, the default 'lobby time' is 45 seconds, and each extension is 15 seconds.
Issue:
I have decided to use Java's Timers. When the 'lobby' starts, a TimerTask is scheduled for 45 seconds from now:
UnoStartTimer.schedule(new UnoStartTask(), 45000);
When someone wants to extend the delay, this is the pseudocode I want to run:
UnoStartTimer.reschedule(UnoStartTimer.getNextScheduledTime() + 15000);
A quick look at the javadoc indicates that such an easy solution, with the Timer class, is not present.
Here's the possibilities I came up with:
- I should use a different scheduling class (I think I saw something called ScheduledExecutorService in an answer to another question, but it didn't immediately seem like it was the solution to my problem)
- There is a way to do this with Timer, and I'm blatantly overlooking it
- There isn't an easy way to do this
So, which one is it?