Scheduled task using timer

C

cksanjose

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
 
R

Roedy Green

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

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
 
P

Piotr Kobzda

cksanjose said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top