我在评论中读到这个答案 https://stackoverflow.com/questions/2212848/how-to-genarate-random-numbers/2212887#2212887以及许多其他有关日程安排的问题(抱歉,没有参考资料)java.util.Timer
已弃用。我真的希望不会,因为我使用它作为 Java 中安排事情的简单方式(而且它工作得很好)。但如果它被弃用,我会去其他地方寻找。然而,快速浏览一下1.6 的 API 文档 http://java.sun.com/javase/6/docs/api/没有说任何关于它被弃用的事情。Sun的文章中甚至没有提到这一点已弃用列表 http://java.sun.com/javase/6/docs/api/deprecated-list.html.
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?
正如其他人提到的,不,它并没有被弃用,但我个人总是使用ScheduledExecutorService http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html相反,它提供了更丰富的 API 和更大的灵活性:
-
ScheduledExecutorService
允许您指定线程数,而Timer
始终使用单线程。
-
ScheduledExecutorService
可以构造为ThreadFactory http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadFactory.html允许控制名称/守护进程状态以外的线程方面(例如优先级,ThreadGroup
, UncaughtExceptionHandler
).
-
ScheduledExecutorService
允许以固定延迟和固定速率安排任务。
-
ScheduledExecutorService
接受Callable
/ Runnable
因为它是工作单元,这意味着您不需要子类化TimerTask
专门使用它;即您可以提交相同的Callable
落实到常态化ExecutorService
or a ScheduledExecutorService
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)