javax.swing.Timer vs java.util.Timer inside of a Swing application
Asked Answered
M

2

8

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?

Magnus answered 29/7, 2014 at 22:2 Comment(4)
Yes.Launch
javax.swing.Timer runs on EDT and thus should be used in conjunction with Swing components.Hemostat
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 performancePalladous
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.Absalom
D
17

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.

Disused answered 29/7, 2014 at 22:5 Comment(2)
which one gives better performance? java.util.Timer or javax.swing.Timer? (I mean no lag)Magnus
@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.Disused
Z
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);
Zicarelli answered 29/7, 2014 at 22:10 Comment(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...Palladous

© 2022 - 2024 — McMap. All rights reserved.