InterruptedException after cancel file open dialog - 1.6.0_26

后端 未结 5 606
抹茶落季
抹茶落季 2020-12-31 13:06

The output from the code that follows is:

java.vendor     Sun Microsystems Inc.
java.version    1.6.0_26
java.runtime.version    1.6.0_26-b03
sun.arch.data.m         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 13:34

    I would say this is a small bug in sun.awt.Disposer.

    That class creates the "Java2D Disposer" daemon thread which handles disposing AWT resources of garbage collected objects (mainly AWT windows). Most of the time that thread waits on its reference queue for a new disposable object to be garbage collected. When the thread is interrupted it explicitly prints that exception.

    When the JVM is terminated it interrupts all threads. Under some circumstances - which are apparently influenced by the usage of JFileChooser and the subsystems initialized by it - some threads still get a chance to run after this interruption. And in this case an InterruptedException is thrown in the "Java2D Disposer" thread because it was waiting on a lock. It would be better if it ignored that exception during shutdown.

    As a workaround, replace

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    with

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosed(WindowEvent e) {
            PrintStream nullStream = new PrintStream(new OutputStream() {
                public void write(int b) throws IOException {
                }
    
                public void write(byte b[]) throws IOException {
                }
    
                public void write(byte b[], int off, int len) throws IOException {
                }
            });
            System.setErr(nullStream);
            System.setOut(nullStream);
            System.exit(0);
        }
    });
    

提交回复
热议问题