myborobudur
myborobudur

Reputation: 4435

Periodically schedule timer task

Just to be sure:

void java.util.Timer.schedule(TimerTask task, Date firstTime, long period)

If my task takes longer then the period parameter then the timer doesn't start a new parallel thread, right? It waits until the task has finished and starts right after.

Upvotes: 0

Views: 99

Answers (2)

kiruwka
kiruwka

Reputation: 9480

You are right. From the documentation :

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.

I suggest you use ExecutorService if you expect your tasks may take longer and should be queued.

Upvotes: 3

Abhijith Nagarajan
Abhijith Nagarajan

Reputation: 4030

Two things, first you should really be using ScheduledExecutorService or Schedulers like quartz for scheduling jobs. Regarding your question, period is the time in milli seconds between successive task execution.

Consider an example where you have scheduled a task to start at 10:00 AM with the delay of 10 mins. Let's say if the first execution takes 10 mins to complete i.e., 10:10 AM then the second execution will start at 10:20 AM.

Upvotes: 1

Related Questions