TimerTask, scheduleAtFixedRate doesn't work parallel

T

tkonrath

Hi.

I have a problem according to the TimerTask - functionality of Java
1.4.2.

I have two timer-tasks which I want to start at the same time each day
(e.g. at 1 a.m.) in my web-application. I configured both well. I used
the scheduleAtFixedRate method to from the Timer class, because I must
be sure that these tasks are not running during the day when a lot of
users are online. So using the method schedule is not possible for me.

I have recognized that the timer tasks are not running parallel. They
are called like step by step.

So may question is, why are they doing so? I have not found any
information about this behaviour in the Internet.

Is it possible to execute timer tasks parallel using
scheduleAtFixedRate or is it just impossible?

Thanks for any hints.
cu,
Tom
 
T

Thomas Weidenfeller

tkonrath said:
I have recognized that the timer tasks are not running parallel. They
are called like step by step.

So may question is, why are they doing so? I have not found any
information about this behaviour in the Internet.

Really? From the Timer API documentation (emphasis by me):

"Corresponding to each Timer object is a single background thread that
is used to execute all of the timer's tasks, *sequentially*."

In general, I would recommend to use the operating system specific
feature to schedule such tasks, and not re-inventing the wheel if not
absolutely necessary.

If you really want a Java solution, there is some scheduling frame work
out there which you could either use or study to build your own:

http://www.opensymphony.com/quartz/

/Thomas
 
T

Thomas Hawtin

tkonrath said:
I have recognized that the timer tasks are not running parallel. They
are called like step by step.
Is it possible to execute timer tasks parallel using
scheduleAtFixedRate or is it just impossible?

There is one thread per Timer. So if you schedule multiple tasks with a
single timer, they will be executed one-by-one. The easy solution is to
use multiple timers. Alternatively, immediately the task is fired, pass
it off to a thread pool.

If you move to 5.0 (it has been out for a year already), then the
concurrency library offers more sophisticated features.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html

If you are sticking to 1.4 for not, but will be upgrading to 5.0, there
is a backport of nearly all the java.util.concurrent API.

http://www.mathcs.emory.edu/dcl/util/backport-util-concurrent/

Tom Hawtin
 
T

tkonrath

Thanks for your answer and your "emphasis" of the Timer API
documentation. Somehow I haven't read this part of the documentation...

We don't want to use a scheduling frame work because the timer tasks
are just a very small part of our product and up to now it is no big
problem that they are processed sequentially.

But when it will be a real problem for us, we will might use the
scheduling frame work you mentioned or we will just use a own Timer for
each task as Tom Hawtin mentioned it below.

Thanks for your help,
Thomas
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top