How do I manage Java timer tasks?

I'm looking for some guidance on how to manage multiple timer tasks. I'd like to be able to dynamically create timers and then when each timer is finished, it'll reset itself.


Timer 1 - perform action x - reset to perform action x again in 30 minutes

Timer 2 - perform action y - reset to perfom action y again in 10 minutes

13.10.2009 19:15:56

What you want is the ScheduledExecutorService.

It allows you to schedule tasks to run at a given time or at a given rate.

13.10.2009 19:24:53

The following code creates a timer and executes it every 1000 ms after an initial delay of 500 ms. You can easily define two or more timers this way.

TimerTask task = new TimerTask() {
    public void run() {
        System.out.println( "exec" );

new Timer().scheduleAtFixedRate( task, 500, 1000 );
13.10.2009 19:22:23
Well I'd actually like to reschedule the task after previous task has completed (to ensure they aren't overlapping).
cfans 13.10.2009 19:43:37

Perhaps it could be worth reviewing Quartz Enterprise Job Scheduler

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs.

14.10.2009 10:24:16