InterruptedException after cancel file open dialog - 1.6.0_26

后端 未结 5 571
抹茶落季
抹茶落季 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:28

    The difference I saw between the code in the blog post is the scope of the JFileChooser. In the blog post the object is a local variable within the onClickedButton function. In your example the object is defined at the class level. I assume being a local variable gives the Disposer thread more time to clear the JFileChooser object.

    When I made the the file chooser object a local variable in the actionPerformed method block in your example, the exception did not occur. I did test it around ten times, Both running the application through eclipse and through the command line. The exception did not occur.

    If the exception still occurs, you can initializing a file chooser object in the constructor of GUI but not assigning it to anything. I assume here it acts as an early initialization and disposal of a heavy weight swing object.

    Hope this helps.

    0 讨论(0)
  • 2020-12-31 13:31

    Try to add a System.gc() call when done with JFileChooser. I had some problems with file locks that this call fixed.

    0 讨论(0)
  • 2020-12-31 13:32

    My output for a slightly altered version of your source (now included as an edit to the question itself) is..

    java.vendor     Sun Microsystems Inc.
    java.version    1.6.0_29
    java.runtime.version    1.6.0_29-b11
    sun.arch.data.model     32
    os.name         Windows 7
    os.version      6.1
    os.arch         x86
    Input File Selected: install.ini.
    Input selection cancelled by user.
    Press any key to continue . . .
    

    From your question it seems the important line is:

    Input selection cancelled by user.
    

    But after that I see no InterruptedException in the output.

    0 讨论(0)
  • 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);
        }
    });
    
    0 讨论(0)
  • 2020-12-31 13:51

    I had a similar issue. I fixed it following the advice from this thread

    0 讨论(0)
提交回复
热议问题