Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling multiple tasks using timers

Tags:

java

How can I schedule multiple tasks using java.util.Timer. I want to read multiple files using timers. I think I have to give each file a different TimerTask so that one file gets one instance of TimerTask and other file gets another, but I don't know how to do it. Please help. Thanks in advance. Here is what I'm doing:

    Timer timer = new Timer();
    // repeat the check every second
    timer.schedule(fileWatcherTask, new Date(), 1000);
like image 864
Rookie Avatar asked Jan 31 '12 06:01

Rookie


People also ask

What is the relationship between Timer and TimerTask?

Timer and TimerTask are java util classes that we use to schedule tasks in a background thread. Basically, TimerTask is the task to perform, and Timer is the scheduler.

What is a Timer task?

A task that can be scheduled for one-time or repeated execution by a Timer.

How do you schedule a Timer in Java?

Java Timer schedule() methodThe schedule (TimerTask task, Date time) method of Timer class is used to schedule the task for execution at the given time. If the time given is in the past, the task is scheduled at that movement for execution.


1 Answers

As javadoc of Timer class indicates your tasks should take very few time. In this case you can forget about time clash. If your tasks take more then 0.1 seconds run them in separate thread. I mean use Timer as a trigger that just makes task to start in separate thread.

you can also use quartz scheduler for that refer http://www.mkyong.com/java/quartz-scheduler-example/

if you want to use timer class see the example in following image enter image description here

refer link for more details

like image 92
Hemant Metalia Avatar answered Nov 07 '22 17:11

Hemant Metalia