6

is this better to use javax.swing.Timer inside of a swing application instead of using java.util.Timer?

for example:

Timer timer = new Timer(1000, e -> label.setText(new Date().toString()));
    timer.setCoalesce(true);
    timer.setRepeats(true);
    timer.setInitialDelay(0);
    timer.start();

or

new java.util.Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            label.setText(new Date().toString());
        }
    }, 0, 1000);

is there any difference between this two?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
FaNaJ
  • 1,329
  • 1
  • 16
  • 39
  • 5
    [Yes](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – trashgod Jul 29 '14 at 22:04
  • 2
    `javax.swing.Timer` runs on [`EDT`](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) and thus should be used in conjunction with `Swing` components. – PM 77-1 Jul 29 '14 at 22:05
  • 2
    The Swing `Timer` also allows for the coalescing of timer events, meaning that if a "timer event" exists on the event queue, no new events will be raised until it is handled, this can prevent the saturation of the Event Queue which can lead to poor performance – MadProgrammer Jul 29 '14 at 23:59
  • You should generally avoid `java.util.Timer` as it has certain limitations. If you really need to schedule non-UI background tasks, use a `ScheduledExecutorService` as it’s the more general solution harmonized with the other concurrency tools. – Holger Jul 30 '14 at 09:24

2 Answers2

15

The difference:

A java.util.Timer starts its own Thread to run the task on.

A javax.swing.Timer schedules tasks for execution on the EDT.

Now. Swing is single threaded.

You must access and mutate Swing components from the EDT only.

Therefore, to make changes to the GUI every X seconds, use the Swing timer. To do background business logic use the other timer. Or better a ScheduledExecutorService.

Bear one very important thing in mind; if you spend time on the EDT it cannot spend time updating the GUI.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • 1
    which one gives better performance? java.util.Timer or javax.swing.Timer? (I mean no lag) – FaNaJ Jul 29 '14 at 22:09
  • 4
    @user3767784 this has nothing to do with performance. **At all**. Read my answer more carefully. It is doing work on the EDT that doesn't belong on the EDT that causes lag. – Boris the Spider Jul 29 '14 at 22:10
5

The main difference is that the javax.swing.Timer runs its code on the EDT while the java.util.timer runs on a separate thread. Because of this swing timers are best used if you are manipulating the GUI in any way. Although if you prefer to use a different type of timer then you can still invoke your code on the EDT.

new java.util.Timer().scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            label.setText(new Date().toString());
        }
    });
}, 0, 1000);
BitNinja
  • 1,477
  • 1
  • 19
  • 25
  • 1
    Just beware, The Swing `Timer` also allows for the coalescing of timer events, meaning that if a "timer event" exists on the event queue, no new events will be raised until it is handled, this can prevent the saturation of the Event Queue which can lead to poor performance, where as your example does not... – MadProgrammer Jul 30 '14 at 00:00