[英]java.util.Timer: Is it deprecated?
I read in a comment to this answer and in many other questions about scheduling (sorry, no references) that java.util.Timer
is deprecated. 我读了一篇关于这个答案的评论以及关于不推荐使用java.util.Timer
调度(抱歉,没有引用)的许多其他问题。 I really hope not since I'm using it as the light way to schedule things in Java (and it works nicely). 我真的希望不要因为我使用它作为在Java中安排事情的轻松方式(并且它工作得很好)。 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. 但是,快速查看1.6的API文档并没有说明它被弃用的任何内容。 It's not even mentioned in Sun's Deprecated List . 在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? *另一方面, 如果它没有被弃用 , 那么人们是否可以停止对这个无辜且精彩实施的集合类进行诋毁?
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
因为它提供了更丰富的API和更多的灵活性:
ScheduledExecutorService
allows you to specify the number of threads whereas Timer
always uses a single thread. ScheduledExecutorService
允许您指定线程数,而Timer
始终使用单个线程。 ScheduledExecutorService
can be constructed with a ThreadFactory
allowing control over thread aspects other than the name / daemon status (eg priority, ThreadGroup
, UncaughtExceptionHandler
). 可以使用ThreadFactory
构造ScheduledExecutorService
,以允许控制除名称/守护程序状态之外的线程方面(例如,priority, ThreadGroup
, UncaughtExceptionHandler
)。 ScheduledExecutorService
allows tasks to be scheduled with fixed delay as well as at a fixed rate. ScheduledExecutorService
允许以固定延迟和固定速率ScheduledExecutorService
任务。 ScheduledExecutorService
accepts Callable
/ Runnable
as it's unit of work, meaning that you don't need to subclass TimerTask
specifically to use it; ScheduledExecutorService
接受Callable
/ Runnable
作为它的工作单元,这意味着你不需要专门使用它来子类化TimerTask
; ie you could submit the same Callable
implementation to a regular ExecutorService
or a ScheduledExecutorService
. 即您可以将相同的Callable
实现提交给常规ExecutorService
或ScheduledExecutorService
。 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. Timer类的JavaDoc提到了ScheduledThreadPoolExecutor
并注意到,这个类实际上是 Timer / TimerTask组合的更通用的替代品 。 Nothing else. 没有其他的。 Timer is not deprecated. 不推荐使用Timer。
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. ThreadPoolExecutor,可以额外调度命令在给定延迟后运行,或定期执行。 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(此类扩展)的额外灵活性或功能时,此类优于Timer。
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. 除了Sun的Deprecated List之外 ,您还会在JavaDoc中看到已弃用的类的注释。 For example, the note for StringBufferInputStream says: 例如, 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类。
There is [JDK-8154799] deprecate Timer and TimerTask in the JDK's bug tracker and in mid-2016 JEP 277 stated that java.util.Timer
(and TimerTask
) would be deprecated in JDK 9. [JDK-8154799]在JDK的bug跟踪器中弃用了Timer和TimerTask ,并且在2016年中期JEP 277声明java.util.Timer
(和TimerTask
)将在JDK 9中弃用。
Several Java SE APIs will have a @Deprecated annotation added, updated, or removed. 多个Java SE API将添加,更新或删除@Deprecated注释。 Some examples of such changes are listed below. 下面列出了这些变化的一些例子。
[…] [...]
- add @Deprecated to
java.util.Timer
andTimerTask
将@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发行版中,不会弃用这些类(不推荐使用的类可以在不推荐使用的列表中找到)。
在jdk1.6_10中它没有被弃用,因此不需要替代方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.