InterruptedException after cancel file open dialog - 1.6.0_26
Asked Answered
B

5

10

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.model     32
os.name     Windows XP
os.version  5.1
os.arch     x86
Input selection cancelled by user.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at sun.java2d.Disposer.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

The following code shows the exception on my machine.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JPanel implements ActionListener {

    private final String newline = System.getProperty("line.separator");
    JButton openButton;
    JTextArea log;
    JFileChooser fc;

    public GUI() {
        super(new BorderLayout());

        log = new JTextArea(20,40);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);

        fc = new JFileChooser();

        openButton = new JButton("Open");
        openButton.addActionListener(this);

        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);

        add(buttonPanel, BorderLayout.NORTH);
        add(new JScrollPane(log));

        showProp("java.vendor");
        showProp("java.version");
        showProp("java.runtime.version");
        showProp("sun.arch.data.model");
        showProp("os.name");
        showProp("os.version");
        showProp("os.arch");
    }

    public void showProp(String name) {
        output(name + " \t" + System.getProperty(name));
    }

    public void output(String msg) {
        log.append(msg + newline);
        log.setCaretPosition(log.getDocument().getLength());
        System.out.println(msg);
    }

    public void actionPerformed(ActionEvent e) {
        //Handle open button action.
        int returnVal = fc.showOpenDialog(GUI.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //This is where a real application would open the file.
            output(
                "Input File Selected: " +
                fc.getSelectedFile().getName() +
                ".");

        } else {
            output("Input selection cancelled by user.");
        }
        log.setCaretPosition(log.getDocument().getLength());
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("IDE Output Converter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(new GUI());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

When I run the program the main window opens fine and the program works fine.

However, if you:

  • open the the JFileChooser using the 'Open File' button
  • press cancel and then
  • exit the program

An InterruptedException is thrown. Or if you choose a file and 'Open' it then exit the program the same error is thrown. On this blog the same thing is explained with example code, his solution is to call new JFileChooser(); as soon as possible, which I have done to no effect.

Is this a bug in 1.6.0_26? If so, is there a work-around for that version?

Is it the code? If so, how to fix it? (Looking less likely, with 2 other null results - one of which is now deleted.)

Brittaniebrittany answered 14/10, 2011 at 15:25 Comment(15)
Sorry ... I do same thing with your code but i didn't get any exception.. The code run fine..in all condition..Bedsore
Perhaps it is my environment. I am using Eclipse Indigo Service Release 1 and JRE6.Brittaniebrittany
i am using same except Java6. i am using Java 7. i did change your code even your comment.. just past it and run... may be..some problem with environment....Bedsore
Just tried it again, you have to click open file, click on some random file within the chooser and then cancel and exit then the error occurs. Though even this has now proved intermittent.Brittaniebrittany
Yeah...i did .i tried 20 times with all possible .. situation...but i still fine.. Interrupted Exception come only when a thread in sleep and on that thread run interrupt() method.Bedsore
Your code is write..and its work..i don't know why its show exception in your PCBedsore
Please provide the output from the command line (as an edit) from the source seen in my answer.Sowers
Would the down voter care to share their reason? IMWTK.Sowers
absolutely.. i want to know why..?Bedsore
I have checked your SSCCE on my machine but could not reproduce it: Windows 7 64, java 1.6.0_23-b05. On your environment you can reproduce every time or just sometimes?Guanajuato
I can reproduce most times. I have also reproduced on a second machine also running XP, JRE1.6.Brittaniebrittany
I have edited (shortened) the code. Please check that it reproduces the buggy behavior in your test machine. I am beginning to think this is a bug in that micro-version, though I'd want to see more copy/pasted output from other developers before jumping to any conclusions. Have you searched the bug database?Sowers
@Brittaniebrittany I can reproduce some issue only by generating Network lack, but I cann't get InterruptedException, only freeze durring defalut timout, I opened 200 in same time, nothing happened, on three differents version for JDK6Marcasite
works fine with me 1.6.0_26-b03Converge
check if there's no cyclic dependency between an instance of GUI and action listener, for how-to use weak references in Swing see here: lucamasini.net/Home/java-in-general-/…Calva
W
12

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);
    }
});
Waac answered 26/10, 2011 at 9:53 Comment(6)
I understand (mostly) what you are saying and it makes sense that this is the cause of the exception. I have made the updates as per your suggestion but I still get: Exception while removing reference: java.lang.InterruptedException java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at sun.java2d.Disposer.run(Unknown Source) at java.lang.Thread.run(Unknown Source)Brittaniebrittany
Sorry, the exception is printed, not propagated. I've changed the code snippet, please try again.Waac
thumbs up. Checked the OpenJDK code and your analysis is sweet.Converge
It is unfortunate that the OP seems to have vanished. I was wanting to hear a report of how this code runs on the problem machine(s).Sowers
Yes, it would be nice to confirm that the exception is not printed anymore. However, my answer is more an explanation than a solution and the code snippet is more or less a hack to hide the exception.Waac
+200. I feel this is the best answer that has been offered (and not deleted) in the time period the bounty is still available (awarded with 5 hrs remaining). The question has not really been solved/answered, mostly due to the disappearance of the OP. :(Sowers
D
4

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

Deputy answered 27/10, 2011 at 18:39 Comment(1)
The accepted answer there: "Your Disposer is blocked in a call to remove() (removing the next platform native resource). This means that the disposer thread (a daemon thread) is not being shutdown naturally when the VM exits" is wrong. The disposer thread is waiting on its reference queue which is its default state.Waac
S
2

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.

Sowers answered 26/10, 2011 at 9:41 Comment(2)
I ran your code and I still get; Exception while removing reference: java.lang.InterruptedException java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at sun.java2d.Disposer.run(Unknown Source) at java.lang.Thread.run(Unknown Source)Brittaniebrittany
Add the exact output of that source as an edit to the question. I have added my source as an edit to your question, since it is more informative and also displays the problem for you (is an example of the problem).Sowers
E
0

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.

Ely answered 27/10, 2011 at 10:39 Comment(0)
T
0

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

Ternate answered 27/10, 2011 at 11:2 Comment(2)
That's voodoo, not a solution. It cannot be predicted what System.gc() will do.Electroacoustics
Hmm not voodoo but a Java bug #4724038 that has been around for ages.Ternate

© 2022 - 2024 — McMap. All rights reserved.