tags:

views:

402

answers:

5

I read in a comment to this answer and in many other questions about scheduling (sorry, no references) that java.util.Timer is deprecated. I really hope not since I'm using it as the light way to schedule things in Java (and it works nicely). But if it's deprecated, I'll look elsewhere. However, a quick look at the API docs for 1.6 doesn't say anything about it being deprecated. It's not even mentioned in Sun's Deprecated List.

Is it officially deprecated* and if so, what should I use instead?


* On the other hand, if it's not deprecated, could people stop badmouthing this innocent and brilliantly-implemented set-o-classes?

+1  A: 

In jdk1.6_10 it's not deprecated, so there is no need for an alternative.

stacker
`java.util.Date` isn't deprecated either, but that's is very much in need of an alternative.
skaffman
Same goes for Observer / Observable.
Adamski
@skaffman what do you use instead of `java.util.Date`? And if they don't deprecate these things, how am I supposed to know not to use them?
Yar
@yar : You know not to use them when you realise they're rubbish. Use your judgement. The defacto replacement for `java.util.Date` is JodaTime.
skaffman
+5  A: 

No. Not all. You may want to use other mechanisms like Quartz for more complex timer requirements, but Timer works perfectly well and is not going anywhere.

Brian Agnew
Thank you for that. I'll check out Quartz, too.
Yar
Yes, Quartz is excellent although the API is not so great. However, the Quartz wrappers provided with Spring make it a lot more useable / flexible.
Adamski
+3  A: 

I think this is a misunderstanding. The Timer class's JavaDoc mentions ScheduledThreadPoolExecutor and notes, that this class is effectively a more versatile replacement for the Timer/TimerTask combination. Nothing else. Timer is no deprecated.

EDIT

another quote from JavaDoc, ScheduledThreadPoolExecutor this time:

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

Andreas_D
Thanks for that. So do you think, in terms of weight of implementation -- speed, memory-use, CPU-use -- Timer is the lightest option?
Yar
Timer is 'lightweight' in that is only has a single background thread. Be aware that both Timer and ScheduledExecutorService use an array internally to hold scheduled tasks. Hence, scheduling 10,000,000 tasks and then cancelling them will leave the array size at 10,000,000. In reality this would rarely be an issue.
Adamski
Honestly, I don't know and don't care about performance in most cases. If I had a performance issue, than exchanging Timer.sleep() by ScheduledThreadPoolExecuter would be on of the last things I looked at...
Andreas_D
Sorry @Andreas_D I don't normally care abut performance either, but I'm using Timer -- successfully -- to create MIDI-note loopers. So if there might easily be 1K scheduled tasks per second... on the other hand, maybe I've gone in an absolutely crazy direction by using Timers.
Yar
+3  A: 

No, it's not deprecated. In addition to Sun's Deprecated List, you'll also see a note in the JavaDoc for a class that has been deprecated. For example, the note for StringBufferInputStream says:

Deprecated. This class does not properly convert characters into bytes. As of JDK 1.1, the preferred way to create a stream from a string is via the StringReader class.

Bill the Lizard
Thanks Bill, the deprecated list was mentioned in the question. I was wondering if there was some insider knowledge that lets people know that Timer is deprecated (or something).
Yar
@yar: Oh, yes, I didn't notice that the word "here" was linked. The JavaDoc for the `Timer` class itself will have a note that will let you know if it's ever deprecated.
Bill the Lizard
Thanks Bill, I admit linking the word `here` is not best-practice for anchor-text :)
Yar
@yar: I'm guilty of doing the same, so I can't complain too loudly. :)
Bill the Lizard
we've all been `<a href='there'>there</a>`
Yar
+8  A: 

As others have mentioned, no it is not deprecated but I personally always use ScheduledExecutorService instead as it offers a richer API and more flexibility:

  • ScheduledExecutorService allows you to specify the number of threads whereas Timer always uses a single thread.
  • ScheduledExecutorService can be constructed with a ThreadFactory allowing control over thread aspects other than the name / daemon status (e.g. priority, ThreadGroup, UncaughtExceptionHandler).
  • ScheduledExecutorService allows tasks to be scheduled with fixed delay as well as at a fixed rate.
  • ScheduledExecutorService accepts Callable / Runnable as it's unit of work, meaning that you don't need to subclass TimerTask specifically to use it; i.e. you could submit the same Callable implementation to a regular ExecutorService or a ScheduledExecutorService.
Adamski
The last benefit might be interesting to me. But aside from that, I'm worried that ScheduledEx... might be "heavier". Thoughts?
Yar
Assuming you create a ScheduledExecutorService with a single thread it is highly unlikely you'll see any difference in performance (despite Timer containing its own priority queue implementation).
Adamski
Thanks for that.
Yar