Why are there two Timer classes in Java(one under

2020-02-05 04:26发布

问题:

I am really confused about this . Java has two Timer classes, one under swing , and one under util ... why is that? Which one should I use if I want to simply run X every Y seconds? Does this mean if I'm building a GUI I have to use the swing version for a timer?

thanks!

回答1:

Here is the difference between javax.swing.Timer and java.util.Timer:

javax.swing.Timer
  • suitable for simpler cases, using low numbers of timers (say less than a dozen)
  • runs ActionListener objects on the event dispatch thread
  • can directly update the GUI, without using EventQueue.invokeLater
  • if the task runs entirely in the event dispatch thread (that is, if it does not spawn a worker thread), then the GUI will remain responsive only if the task does not take very long (say under 300 milliseconds)

java.util.Timer

  • more scalable than javax.swing.Timer, and with additional scheduling features
  • runs TimerTask objects on a private thread
  • needs to use EventQueue.invokeLater to update the GUI

You can use Swing timers in two ways:

  • To perform a task once, after a delay. For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it.
  • To perform a task repeatedly. For example, you might perform animation or update a component that displays progress toward a goal.

Here is the sources for above information http://www.javapractices.com/topic/TopicAction.do?Id=160 and http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

Which one should I use if I want to simply run X every Y seconds?

Depending upon what you are interacting with. If you are interacting with GUI then use javax.swing.Timer , else use java.util.Timer.

Does this mean if I'm building a GUI I have to use the swing version for a timer?

YES



回答2:

Swing version is for rendering swing components. If you just need to time, use util.



回答3:

You're kind of correct. It's recommended that if you're going to do UI work that will be affected by a timer you should use the swing component. The util timer cannot set UI elements itself. Here is a nice comparison.



回答4:

In v 1.3, another Timer class was added to the Java platform: java.util.Timer. Both it and javax.swing.Timer provide the same basic functionality, but java.util.Timer is more general and has more features. The javax.swing.Timer has two features that can make it a little easier to use with GUIs. First, its event handling metaphor is familiar to GUI programmers and can make dealing with the event-dispatching thread a bit simpler. Second, its automatic thread sharing means that you don't have to take special steps to avoid spawning too many threads. Instead, your timer uses the same thread used to make cursors blink, tool tips appear, and so on.

You can find further documentation and several examples of using timers by visiting How to Use Timers, a section in The Java Tutorial. For more examples and help in choosing between this Timer class and java.util.Timer, see Using Timers in Swing Applications, an article in The Swing Connection.

From the official documentation.



回答5:

If you have a simple, quick task that needs to interact with the swing framework, then it is simpler to use javax.swing.Timer

For almost every other case - even GUI appliactions you should use java.util.Timer If you have a GUI then you have to handle integration with the swing event dispatch thread the same way any other task would by using EventQueue.invokeLater to update the GUI as mentioned above

Generally when you start, the first few timer events may seem quick and unlikely to effect performance, but as the requirements change, they will take longer and more will appear and the demands of the GUI itself will grow. It is a better practice to avoid the rework by just starting outside the swing environment - otherwise your GUI will quickly appear "sluggish" or "unusable"



标签: java swing timer