Occasional InterruptedException when quitting a Swing application

前端 未结 17 1123
心在旅途
心在旅途 2020-12-05 04:55

I recently updated my computer to a more powerful one, with a quad-core hyperthreading processor (i7), thus plenty of real concurrency available. Now I\'m occasionally

相关标签:
17条回答
  • 2020-12-05 05:33

    Threads in java only terminate when all of run() methods finish execution. Otherwise you will always have those Thread extended objects in VM messing around. You have to finish all the threads before exiting the Application.

    Also consider that when you create your jFrame you are starting a Thread (CURRENT_THREAD I believe)

    0 讨论(0)
  • 2020-12-05 05:34

    There is the errors again sometimes but this seems to work fine :

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        svExitMenuItem.removeActionListener(null);
        windowClosing(null);//i add it to clear and call the garbadge collector...
        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(etscampide.ETScampIDEApp.class).getContext().getActionMap(ETScampIDEView.class, this);
        actionMap.get("quit").actionPerformed(null);
    }
    
    0 讨论(0)
  • 2020-12-05 05:35

    I was getting a similar error and somehow this worked for me:

    private static void createAndShowGUI() {
      JFrame jf = new MyProgram();
      jf.setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
            public void run() {
              createAndShowGUI();
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-05 05:37

    If you were using the Swing App Framework, you could override Application.exit() to perform cleanup, or add an ExitListener as well. Otherwise, you could also add a shutdown hook, Runtime.getRuntime().addShutdownHook() to your app.

    0 讨论(0)
  • 2020-12-05 05:37

    Try use EventQueue.invokeLater() to close your Swing.

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