Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is the code:

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();
  }
}

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?

ADDED:

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?

ADDED 2:

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?

like image 984
Roman Avatar asked Jul 10 '26 12:07

Roman


2 Answers

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.

like image 67
Frank Avatar answered Jul 13 '26 09:07

Frank


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.

like image 27
sisis Avatar answered Jul 13 '26 10:07

sisis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!