- Avoid using
java.util.Timer
. See Checking if a Java Timer is cancelled Checking if a Java Timer is cancelled and java.util.Timer: Is it deprecated? java.util.Timer: Is it deprecated? Instead use aScheduledExecutorService
. - When changing to a
ScheduledExecutorService
, don't cancel or shutdown yourScheduledExecutorService
, instead cancel theFutureTask
that you will get when scheduling a task. - Don't decrease a counter once a second. Nothing guarantees that it will be called with exactly 1000 ms periodicity.
In your reset
method, use System.nanoTime()
(not System.currentTimeMillis
not System.currentTimeMillis
) to calculate when the time is up, such as:
this.targetTime = System.nanoTime() + seconds * 1_000_000_000;
Then calculate in getSecondsLeft
, by using System.nanoTime()
again, how many seconds remain until the target nanosecond value has been reached.
- Avoid using
java.util.Timer
. See Checking if a Java Timer is cancelled and java.util.Timer: Is it deprecated? Instead use aScheduledExecutorService
. - When changing to a
ScheduledExecutorService
, don't cancel or shutdown yourScheduledExecutorService
, instead cancel theFutureTask
that you will get when scheduling a task. - Don't decrease a counter once a second. Nothing guarantees that it will be called with exactly 1000 ms periodicity.
In your reset
method, use System.nanoTime()
(not System.currentTimeMillis
) to calculate when the time is up, such as:
this.targetTime = System.nanoTime() + seconds * 1_000_000_000;
Then calculate in getSecondsLeft
, by using System.nanoTime()
again, how many seconds remain until the target nanosecond value has been reached.
- Avoid using
java.util.Timer
. See Checking if a Java Timer is cancelled and java.util.Timer: Is it deprecated? Instead use aScheduledExecutorService
. - When changing to a
ScheduledExecutorService
, don't cancel or shutdown yourScheduledExecutorService
, instead cancel theFutureTask
that you will get when scheduling a task. - Don't decrease a counter once a second. Nothing guarantees that it will be called with exactly 1000 ms periodicity.
In your reset
method, use System.nanoTime()
(not System.currentTimeMillis
) to calculate when the time is up, such as:
this.targetTime = System.nanoTime() + seconds * 1_000_000_000;
Then calculate in getSecondsLeft
, by using System.nanoTime()
again, how many seconds remain until the target nanosecond value has been reached.
- Avoid using
java.util.Timer
. See Checking if a Java Timer is cancelled and java.util.Timer: Is it deprecated? Instead use aScheduledExecutorService
. - When changing to a
ScheduledExecutorService
, don't cancel or shutdown yourScheduledExecutorService
, instead cancel theFutureTask
that you will get when scheduling a task. - Don't decrease a counter once a second. Nothing guarantees that it will be called with exactly 1000 ms periodicity.
In your reset
method, use System.nanoTime()
(not System.currentTimeMillis
) to calculate when the time is up, such as:
this.targetTime = System.nanoTime() + seconds * 1_000_000_000;
Then calculate in getSecondsLeft
, by using System.nanoTime()
again, how many seconds remain until the target nanosecond value has been reached.