Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Scheduled task using timer

Thread view: 
cksanjose - 31 Oct 2007 20:00 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.