tags:

views:

1586

answers:

7

I am planning to use this to poll a database which can take close to 10-15 minutes or more, so should I be using timers for this.

+4  A: 

I'm assuming you talking about the java.util.Timer, not the Swing specific ones?

If I remember correctly, that timer simply defers execution to a single background thread that essentially serves every timer instance in the JVM, so the problem could be that one timer hogs that thread for every other timer.

So quickly would depend on the rest of your application and whether you use other timers.

Generally, I avoid using it for anything serious, or I just have it spawn a new thread.

Uri
It's one thread per timer, which then sequentially runs each TimerTask associated to that timer.
Robin
Uri - per the Timer javadoc: "Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially." Meaning one thread per Timer, and not one thread which serves all timers.
Yuval A
+1  A: 

The timer TASK should execute quickly, the polling interval is irrelevant to its usage. Make sure you aren't confusing the two.

jonathan.cone
+8  A: 

If you want the task to complete quickly but what you need to do actually takes longer, you could split it up into two pieces. Have one that runs when the Timer fires, so you get things happening at the right time, but then defer the serious processing to another function (say, in a different thread) that can take as long as it needs. From the API, the problem with Timer tasks taking too long is that they hog the thread, possibly delaying subsequent tasks, so moving the time-consuming processing to another thread should avoid that problem. To answer the question in your title, "quickly" would mean "it should finish before the Timer needs to fire again".

lemnisca
+4  A: 

Though if you spawn a thread to do the work from the timer task, beware the situation where the time fires more quickly than the threads complete, causing an ever degrading workload.

Software Monkey
+1  A: 

In this context, it means you must have completed your work until the next timer event happens because otherwise, the next event won't be processed. Since the timer class is global, you can never be sure who else is using it (for example, your DB driver might be using it to implements timeouts).

If you have to do a long work, use the timer to "kick off" another thread (add an item to a work queue or start a second thread in the timer event handler). This will quickly free the timer for the next event.

Aaron Digulla
A: 

java.util.concurrent.ScheduledThreadPoolExecutor is the "new java.util.Timer". You can have a number (a pool) of threads, so you can accommodate long running tasks. Even so, you might want to consider checking that you aren't overlapping too much.

And of course, you can have multiple Timers, and distribute your TimerTasks appropriately.

Tom Hawtin - tackline
A: 

The answer is, it depends. If you are only going to have this one polling task run, and the interval is more than sufficient for each run to finish before the next one begins, then this solutions will work fine.

The problem is when there is more than one TimeTask on the same Timer, since they execute sequentially on the same thread. Therefore a long running task would cause the other tasks to wait for long periods of time, which defeats the purpose of creating timed tasks.

There are better solutions for the more complex scenario in the java.util.concurrent package as well as other libraries like Quartz.

Robin