Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there two Timer classes in Java(one under javax.swing, one under java.util )?

Tags:

java

timer

swing

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!

like image 659
Caffeinated Avatar asked Mar 21 '13 19:03

Caffeinated


People also ask

What is javax swing timer?

A Swing timer (an instance of javax. swing. Timer ) fires one or more action events after a specified delay. Do not confuse Swing timers with the general-purpose timer facility in the java. util package.

What is the use of timer class in Java?

Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time. Each task may be scheduled to run once or for a repeated number of executions.

What is timer and TimerTask in Java?

Timer and TimerTask are java util classes that we use to schedule tasks in a background thread. Basically, TimerTask is the task to perform, and Timer is the scheduler.

What is timer thread in Java?

Timer is a utility class that can be used to schedule a thread to be executed at certain time in future. Java Timer class can be used to schedule a task to be run one-time or to be run at regular intervals.


1 Answers

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

like image 68
Vishal K Avatar answered Oct 05 '22 19:10

Vishal K