I have a standalone application that is scheduled to run every 15
minutes. I'm using the Timer class for scheduling. When it starts
running, it checks a table to see if there's any task that is waiting
to be processed. It is possible that one of these tasks will run for
about 3 hours. Does it wait for this task to finish before it checks
again if there's another task to perform? Should I use
"scheduleAtFixedRate" instead of the "schedule" method so that it
checks for a task every 15 minutes regardless if there's a task
running? What I would like is for the application to check every 15
minutes and process any task that is waiting.
Thanks for any help.
Cesar
Roedy Green - 31 Oct 2007 21:40 GMT
>I have a standalone application that is scheduled to run every 15
>minutes. I'm using the Timer class for scheduling. When it starts
[quoted text clipped - 9 lines]
>Thanks for any help.
>Cesar
Timers use a single thread to do all the work, so you can't have
anything on them that takes longer than then time to the next event.
To get around that, your timer event could trigger a background task
that runs in parallel with the timer.
See http://mindprod.com/jgloss/timer.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Piotr Kobzda - 31 Oct 2007 23:19 GMT
> What I would like is for the application to check every 15
> minutes and process any task that is waiting.
I suggest to use java.util.concurrent tools instead of Timer for that.
You just need two things:
-- A single thread ScheduledExecutorService (i.e.
Executors.newSingleThreadScheduledExecutor()) with one scheduled task
(most likely "scheduleAtFixedRate" with a period of 15 minutes);
-- A thread pool (e.g. Executors.newCachedThreadPool()) for execution
of your tasks.
The scheduled task should simply "submit" each new task into tasks
thread pool.
Refer to API documentation for details.
piotr