java – 什么阻止程序退出?

我是这样写的.

public static void main(String[] args){
    Thread thread = new Thread(() -> {
        while(true){
            try{
                // do something
                Thread.sleep(10);
            }catch(InterruptedException ex){
                System.out.println("ABC");
                break;
            }
        }
    });
    JFrame frame = new JFrame();
    frame.setSize(1280,720);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter(){
        @Override
        public void windowClosed(WindowEvent e){
            thread.interrupt();
        }
    });
    thread.start();
    frame.setVisible(true);
}

当我点击窗口的“关闭”按钮时,
该计划花了几秒钟才结束.
什么阻止程序退出?
如何在不使用JFrame.EXIT_ON_CLOSE的情况下立即关闭程序?

我认为我的线程(而(真)线程)立即结束
因为单击“关闭”按钮后很快就会显示“ABC”.

谢谢.

编辑

没有我的线程发生同样的事情.

public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setSize(1280,720);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}

EDIT2

public static void main(String[] args){
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        Thread[] threads = new Thread[5];
        Thread.enumerate(threads);
        System.out.println(Arrays.toString(threads));
        System.err.println(LocalDateTime.now() + " SHUTDOWN");
    }
    ));
    JFrame frame = new JFrame();
    frame.setSize(1280,720);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosed(WindowEvent e){
                Thread[] threads = new Thread[5];
                Thread.enumerate(threads);
                System.out.println(Arrays.toString(threads));
                System.err.println(LocalDateTime.now() + " CLOSE");
            }
        });
    frame.setVisible(true);
}  

输出是:

[Thread[AWT-EventQueue-0,6,main], Thread[DestroyJavaVM,5,main], null, null, null]
2016-08-02T19:04:50.465 CLOSE
[Thread[DestroyJavaVM,5,main], Thread[Thread-0,5,main], null, null, null]
2016-08-02T19:04:51.762 SHUTDOWN
最佳答案
虽然通常应谨慎使用System.exit(),但Swing documentation明确指出它是方案中正确的方法,如问题所示:

When the last displayable window within the Java virtual machine (VM)
is disposed of, the VM may terminate. Note, however, that there can
be a delay before the program exits automatically
, and that under
some circumstances the program might keep running. It is quicker and
safer to explicitly exit the program using System.exit(int)
.

以下示例未显示关闭操作和关闭挂钩之间的延迟.删除System.exit(0)调用时,延迟大约.可以观察到1,3秒:

package com.example.swing;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.time.LocalDateTime;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ExitDelay {
    public static void main(String[] args){

        Runtime.getRuntime().addShutdownHook(new Thread(() -> System.err.println(LocalDateTime.now() + " SHUTDOWN") ));

        SwingUtilities.invokeLater(() -> {

            JFrame frame = new JFrame();
            frame.setSize(1280,720);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.addWindowListener(new WindowAdapter(){
                @Override
                public void windowClosed(WindowEvent e){
                    System.err.println(LocalDateTime.now() + " CLOSE");

                    // Do all required cleanup stuff.
                    // check that threads are done, close files, commit transactions etc.
                    // ...

                    System.exit(0);
                }
            });
            frame.setVisible(true);

        });
    }
}

转载注明原文:java – 什么阻止程序退出? - 代码日志