[en] Fix irregularity of Java Timer and ScheduledThreadPoolExecutor

Using Java Timer class one can schedule a task for recurring execution. Even though ScheduledThreadPoolExecutor makes it possible to create timers with nanosecond resolution (but beware of underlying operating system clock precision!), regularity of task execution is by default awful.

I have run into this once and had to explain the customer that it’s not fixable. When I measured the delay between timer executions, etc. it turned out that doing as much as new Thread(runnable).start() usually takes 1-5 milliseconds, but at times it takes two orders of magnitude longer: even half a second!

Same customer, other project and this time I thought of a reason for this behaviour: garbage collection. I started with calling System.gc() explicitly inside measured code and it takes 200-500 milliseconds! So it must be it.

Is there a solution? Of course there is. It’s called The Concurrent Low Pause Collector and has been in Java since version 5. Read the linked article for many, many details, but shortly just add:


to the usual java -jar command and the garbage collection will be done in parallel with your code execution. Of course it’s no golden bullet, it makes overall performance worse (now instead of 1-5 milliseconds, a single loop pass takes 4-10 milliseconds), but for this particular use, it makes Timer regularity better by two orders of magnitude.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *