Java – interruptedexception cancel file open dialog box – 1.6 0_ twenty-six
The output of the following code 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)); 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 and the program runs normally
However, if you:
>Use the 'open file' button to open Jfilechooser > press cancel and then > exit the program
Throw interruptedexception Or if you select a file, open it, and exit the program, the same error is thrown On this blog, the same thing is explained with sample code. His solution is to call the new jfilechooser(); As soon as possible, I did it without effect
This is 1.6 0_ 26 mistakes? If so, is this version valid?
Is it code? If so, how to solve it? (unlikely, there are two other empty results – one of which is now deleted.)
Solution
I would say this is a small bug in sun awt. Disposer.
This class creates a "Java2D dispenser" daemon thread, which handles the AWT resource processing of garbage collection objects (mainly AWT windows) Most of the time a thread waits for its reference queue to be garbage collected as a new disposable object When the thread is interrupted, it will print out the exception explicitly
When the JVM is terminated, it interrupts all threads In some cases - obviously affected by the use of Jfilechooser and the subsystems initialized by it - some threads still have the opportunity to run after this interrupt And in this case, an interruptedexception is thrown in the "Java2D dispenser" thread because it is waiting for locking It would be better to ignore this 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); } });