Tickers in Minitab
Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.
When we run this program the ticker should tick 3 times before we stop it.
In this Java version, we use a Timer
and TimerTask
to replicate the functionality of Go’s ticker. The Timer
schedules the TimerTask
to run every 500 milliseconds. We use a CountDownLatch
to keep the main thread alive until we’re done with the ticker.
The Thread.sleep(1600)
simulates waiting for 1600 milliseconds before stopping the ticker. After that, we call timer.cancel()
to stop the ticker, which is equivalent to ticker.Stop()
in the Go version.
Note that Java’s Timer
is not as precise as Go’s ticker for short intervals, and for more accurate timing, you might want to consider using ScheduledExecutorService
in a real-world application.