简体   繁体   中英

Java Timer: Terminating under weird circumstances, explanation?

I'm writing a very simple program that does something every 5 minutes. I'm currently trying to set up a Timer and test it by simply having it increment a number. I copied some code including the code in question(CIQ) from a related question and ended up with what is below. I noticed one behavior that I can't explain for the life of me:

If the CIQ is left in place, the program runs as expected, no matter what is the values of delay. However, if the CIQ is removed, as I tried to do in order to cut off parts that I didn't need, such as windows, the program stops all by itself for values roughly < 900. It ran just fine for an hour for 800, it counts to about two or three for 1000, and terminates without incrementing at all for anything higher. What is happening here? CIQ, so far as I understand, specifies the style of a dummy window and then packs it. What does that have to do with a timer running somewhere in the background, and why does this happen in the range of values described? I've read that JFrame is a top-level container for Swing, but if Swing stuff needs it to work, why does the timer sometimes works at all without it? Does it have to do with garbage collection or something?

Time:

    public class Time {

    public Time() {
        EventQueue.invokeLater(() -> {
            //<codeInQuestion>
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            //</codeInQuestion>

            ActionListener taskPerformer = new ActionListener() {
                int testCount = 0;

                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Time passed: " + testCount);
                    testCount++;
                }
            };
            Timer timer = new Timer(1000, taskPerformer);
            timer.start();
        });
    }
}

Main:

    public class Main {
    public static void main(String[] args) {
        Time timer = new Time();
    }
}

the program stops all by itself for values roughly < 900.

The application will only keep running if the Event Dispatch Thread has been started.

Since you don't make the frame visible there is no reason for the GUI to keep executing.

A TImer places events on the EDT when it fires. For values < 900, the Timer fires before the GUI closes so the Event Dispatch Thread is started.

Your CIQ must also add some event to the EDT to prevent the GUI from closing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM