java.util.Timer:是否已弃用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2213109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.Timer: Is it deprecated?
提问by Dan Rosenstark
I read in a comment to this answerand in many other questions about scheduling (sorry, no references) that java.util.Timeris 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.6doesn't say anything about it being deprecated.It's not even mentioned in Sun's Deprecated List.
我阅读了对此答案的评论以及有关java.util.Timer已弃用的调度(抱歉,没有参考)的许多其他问题。我真的希望不会,因为我将它用作在 Java 中安排事情的轻松方式(并且它运行良好)。但如果它已被弃用,我会寻找其他地方。但是,快速浏览1.6的API 文档并没有说明它已被弃用。它甚至没有在 Sun 的弃用列表中提及。
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?
*另一方面,如果它没有被弃用,人们能不能停止对这个天真而出色实现的 set-o-classes 说坏话?
采纳答案by Martin
There is [JDK-8154799] deprecate Timer and TimerTaskin the JDK's bug tracker and in mid-2016 JEP 277stated that java.util.Timer(and TimerTask) would be deprecated in JDK 9.
有[JDK-8154799]弃用Timer和TimerTask在JDK的错误跟踪和中旬2016 JEP 277表示java.util.Timer(和TimerTask)将被在JDK 9弃用。
Several Java SE APIs will have a @Deprecated annotation added, updated, or removed. Some examples of such changes are listed below.
[…]
- add @Deprecated to
java.util.TimerandTimerTask
一些 Java SE API 将添加、更新或删除 @Deprecated 注释。下面列出了此类更改的一些示例。
[…]
- 将@Deprecated 添加到
java.util.Timer和TimerTask
However, in the JDK 9 release, those classes are not deprecated (deprecated classes can be found in the Deprecated List).
但是,在 JDK 9 版本中,这些类并未被弃用(已弃用的类可以在弃用列表中找到)。
回答by Adamski
As others have mentioned, no it is not deprecated but I personally always use ScheduledExecutorServiceinstead as it offers a richer API and more flexibility:
正如其他人所提到的,不,它没有被弃用,但我个人总是使用ScheduledExecutorService它,因为它提供了更丰富的 API 和更大的灵活性:
ScheduledExecutorServiceallows you to specify the number of threads whereasTimeralways uses a single thread.ScheduledExecutorServicecan be constructed with aThreadFactoryallowing control over thread aspects other than the name / daemon status (e.g. priority,ThreadGroup,UncaughtExceptionHandler).ScheduledExecutorServiceallows tasks to be scheduled with fixed delay as well as at a fixed rate.ScheduledExecutorServiceacceptsCallable/Runnableas it's unit of work, meaning that you don't need to subclassTimerTaskspecifically to use it; i.e. you could submit the sameCallableimplementation to a regularExecutorServiceor aScheduledExecutorService.
ScheduledExecutorService允许您指定线程数,而Timer始终使用单个线程。ScheduledExecutorService可以通过ThreadFactory允许控制线程方面而不是名称/守护进程状态(例如优先级、ThreadGroup、UncaughtExceptionHandler)来构造。ScheduledExecutorService允许以固定延迟和固定速率安排任务。ScheduledExecutorService接受Callable/Runnable因为它是工作单元,这意味着您不需要TimerTask专门子类来使用它;即您可以将相同的Callable实现提交给常规ExecutorService或ScheduledExecutorService.
回答by Andreas Dolk
I think this is a misunderstanding. The Timer class's JavaDoc mentions ScheduledThreadPoolExecutorand notes, that this class is effectively a more versatile replacementfor the Timer/TimerTask combination. Nothing else. Timer is not deprecated.
我认为这是一种误解。Timer 类的 JavaDoc 提到ScheduledThreadPoolExecutor并指出,该类实际上是Timer/TimerTask 组合的更通用的替代品。没有其他的。不推荐使用计时器。
Another quote from JavaDoc, ScheduledThreadPoolExecutor this time:
这次引用了 JavaDoc,ScheduledThreadPoolExecutor:
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.
一个 ThreadPoolExecutor 可以额外安排命令在给定延迟后运行,或定期执行。当需要多个工作线程时,或者需要 ThreadPoolExecutor(此类扩展)的额外灵活性或功能时,此类比 Timer 更可取。
回答by Brian Agnew
回答by Bill the Lizard
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 StringBufferInputStreamsays:
不,它没有被弃用。除了 Sun 的Deprecated List 之外,您还会在 JavaDoc 中看到关于已被弃用的类的注释。例如,StringBufferInputStream的注释说:
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.
已弃用。此类未正确将字符转换为字节。从 JDK 1.1 开始,从字符串创建流的首选方法是通过 StringReader 类。
回答by stacker
In jdk1.6_10 it's not deprecated, so there is no need for an alternative.
在 jdk1.6_10 中它没有被弃用,所以不需要替代方案。

