Rookie
Rookie

Reputation: 8790

Scheduling multiple tasks using timers

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);

Upvotes: 8

Views: 10398

Answers (1)

Hemant Metalia
Hemant Metalia

Reputation: 30638

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

Upvotes: 6

Related Questions