ExecutorService 与 Swing 计时器 [英] ExecutorService vs Swing Timer

查看:35
本文介绍了ExecutorService 与 Swing 计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在阅读 Filthy Rich Clients 并注意到,虽然 Java 的版本是 6,但没有提到并发框架.所以,他们谈论的是 java.util.Timer 和 javax.swing.Timer 而不是 ExecutorService.

I've been reading Filthy Rich Clients lately and noticed that, although the version of Java is 6, there is no mention of the Concurrent Framework. So, they talk about java.util.Timer and javax.swing.Timer but not about the ExecutorService.

我在问题Java Timer vs ExecutorService"中了解了 ExecutorService 的优势,并且决定使用后者而不是前者.但是这本书谈到了 javax.swing.Timer 以及它特定于 Swing 开发的优势.

I read about the advantages of ExecutorService in the question "Java Timer vs ExecutorService" and decided to use the latter over the former. But the book talks about javax.swing.Timer and it's advantages of being specific for Swing development.

那么,这是否意味着,对于 Swing 开发(动画按钮等),javax.swing.Timer 仍然是更好的选择,还是在新的 Concurrent Framework 中是否有相关类可以替代它?

So, does this mean that, for Swing development (animating buttons etc.), javax.swing.Timer is still a better choice or is there a relevant class in the new Concurrent Framework that replaces it?

推荐答案

Swing Timer 至少在 EDT 上运行,因此您不必通过调用 invokeLater 来包装所有内容.它还与 Swing 很好地结合在一起,因为它使用了 Actions、ActionListeners 和其他与 Swing 相关的类.

Well the Swing Timer at least runs on the EDT so you do not have to wrap everything with calls to invokeLater. It also ties nicely in with Swing as it uses Actions, ActionListeners and other Swing related classes.

对于 Swing 相关的任务,我会坚持使用 Swing Timer,对于不涉及更新 GUI 的事情,我会使用新的并发包.

I'd stick with Swing Timer for Swing related tasks and use the new concurrent package for things that does not involve updating the GUI.

查看在 Swing 应用程序中使用计时器可能包含更多信息来摆动(抱歉)决定.

Have a look at Using Timers in Swing Applications as it might contain more information to swing (sorry) the decision.

这篇关于ExecutorService 与 Swing 计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆