Timer managment


I have an UI with a clock toggling the display of a the ‘:’ using a TimerTask. This was working but now I have another TimerTask somewhere else, this task takes a lot of time to be executed (about 20s) but during this execution my clock stops to toggle.

Do you have any idea why?

Thank you!

Hi Marc,

One Timer is linked to one and only one Java Thread, so while it is executing a Task, it cannot execute another one on the same time.

If you are using the Timer from the ServiceLoader, it returns a Singleton (so the same timer). You could execute your long task in another Timer, or a dedicated Thread or an Executor.

When creating new Timer keep in mind that it will generate a new Thread, so for quick TimerTask, it is recommended to use the ServiceLoader’s Timer.