timer tasks "hogging" avoidance

like in the subject:

I know that java.util.Timer is not appropriate for managing longer tasks, but I

have a problem that actually needs sth like that: I have to periodically (with a

constant period) check for mail on a certain server. When I make a new timer

with period = 15 sec. and checking mail takes 20 seconds than next mail check

is executed instantly, when I want it to be executed after 15 sec. javax.swing.Timer

has a very useful method restart, which could do the thing, but this timer is even

worse when it comes to longer tasks execution, not mentioning that I use swing to

build gui of my application so I don't want to mess up with EDT.

So is it any other solution than while (true) { mailCheck(); Thread.sleep(); } ?

[795 byte] By [Java_vigenera] at [2007-11-26 17:11:40]
# 1
Have the task schedule its own next execution when it completes.
ejpa at 2007-7-8 23:39:30 > top of Java-index,Core,Core APIs...
# 2

Timer has a single thread to execute all TimerTasks. You either need to use multiple Timer objects for multiple, potentially overlapping tasks, or else use the java.util.concurrent ScheduledThreadPoolExecutor (STPE)in Java 5.

Edited to add: in case I misunderstood the issue. Note that both Timer and STPE can either schedule-at-fixed-rate or schedule-at-fixed-delay. The former establishes the release times as T, 2T, 3T etc so if the task doesn't compete before the next expected release time then it will release again immediately. In contrast fixed-delay defines the next release time as end-of-last-release+T, so no matter how long the task takes the next one will be released T timeunits later.

Message was edited by:

davidholmes

davidholmesa at 2007-7-8 23:39:30 > top of Java-index,Core,Core APIs...