如果我们不生成窗口,为什么Timer不起作用? [英] Why Timer does not work if we do not generate a window?

查看:74
本文介绍了如果我们不生成窗口,为什么Timer不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class TimerSample {
  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();
  }
}

它生成一个窗口,然后定期打印Hello World定时器在终端(命令提示符)。如果我评论此行 new JFrame()。setVisible(true); 应用程序不会向命令行打印任何内容。为什么?

It generates a window and then periodically prints "Hello World Timer" in the terminal (Command Prompt). If I comment this line new JFrame().setVisible(true); the application does not print anything to the command line. Why?

已添加:

我不确定我理解答案正确。据我所知,计时器启动一个新线程。并且这个新线程与主线程同时存在。当主线程完成时(当一切都完成并且没有任何事情可做)时,整个应用程序终止(与定时器创建的新线程一起)。是对吗?

I am not sure that I understand the answers correctly. As far as I understood, the timer starts a new thread. And this new thread exists simultaneously with the "main" thread. When the "main" thread is finished (when everything is done and there is nothing to do anymore) the whole application is terminated (together with the "new" thread created by the timer). Is right?

已添加2:

上述解释仍然没有解释一切。例如,如果我评论 new JFrame()。setVisible(true); 并输入 try {Thread.sleep(20000);在 timer.start()之后的catch(InterruptedException e){}; 。所以,我有点理解。在休眠状态下,我们保持主线程忙,这样定时器创建的线程就可以存在。但是 new JFrame()。setVisible(true); 不占用main。据我所知,它创建了自己的线程(如Timer)。那么,为什么JFrame的线程可以在没有主线程和计时器线程的情况下存在呢?

The above described explanation still does not explain everything. For example, the program works if I comment the new JFrame().setVisible(true); and put try {Thread.sleep(20000);} catch(InterruptedException e) {}; after the the timer.start(). So, I kind of understand that. With the sleep we keep the "main" thread busy so the thread created by the timer can exist. But new JFrame().setVisible(true); do not occupy the "main". As far as I understand it creates its own thread (like Timer). So, why thread of the JFrame can exist without the main thread and thread of timer cannot?

推荐答案

你错过了这一点。 Timer独立于您创建的窗口,当您注释掉该窗口创建行时,它也可以工作。

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.

但是,您看不到的是:您的主程序退出在 timer.start()之后,因此,您的程序执行被终止,并随之进入计时器。

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.

如果在末尾添加 Thread.sleep(20000); (包括所需的异常处理)或需要一些时间的任何其他代码,则可以验证这一点。然后你的计时器工作得很好,即使没有创建任何窗口。

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.

重点是即使没有显示任何内容,JFrame仍保持活动状态,这反过来又保持你的计时器活着。

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

这篇关于如果我们不生成窗口,为什么Timer不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆