Why Timer does not work if we do not generate a window?

天涯浪子 提交于 2019-12-04 12:09:23

You're missing the point. The Timer is independent from the window you created and it works also when you comment out that window creation line.

However, what you failed to see is: your main program exits after timer.start(), hence, your program execution is terminated and along with it goes the timer.

You can verify this if you add Thread.sleep(20000); at the end (including the required exception handling), or any other code that takes some time. Then your timer works just fine even without any window being created.

The point is that the JFrame, even when nothing is displayed, remains active, which in turn keeps your timer alive.

The timer function is called in the window event loop (where also stuff like resizing, moving, repainting etc. is taken care of). Apparently if the window is hidden, the event loop is not executed.

This is wrong, check Frank's answer instead.

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially.

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the the timer's cancel method.1

So what does the java.util.Timer has to do with the javax.swing.Timer you might ask?

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 2

So your last reference to the timer goes away before your timer really starts... if you notice your main method completes as soon as you call timer.start(); and unless there is something that will keep your main thread alive until the timer starts, then your timer might never start.

In the first example the JFrame is keeping your main thread alive.

In the second example you sleep long enough for the timer to start and then the timer causes the main thread to stay alive:

public static void main(String args[]) {
        //new JFrame().setVisible(true);
        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Hello World Timer");
            }
        };
        Timer timer = new Timer(500, actionListener);
        timer.start();
        try {
            Thread.sleep(500);//<--- the timer will still die
            Thread.sleep(100);//<--- sleep for another 100 and the timer will start printing although you can't rely on it
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!