即使使用 SwingWorker,Java GUI 也会冻结

2024-02-19

我正在尝试使用 SwingWorker 执行一项冗长的任务并使用结果更新 JLabel:

button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        new SwingWorker<String, Void>() {

            @Override
            protected String doInBackground() throws Exception {
                return doCalculation();
            }

            protected void done() {
                try {
                    label.setText(get());
                } catch (InterruptedException e) {
                    System.out.println("thread was interrupted");
                } catch (ExecutionException e) {
                    System.out.println("there was an ExecutionException");
                }
            }

        }.execute();

    }

});

我可以根据需要多次单击该按钮,Gui 将保持响应,直到两个线程完成,此时 Gui 在线程运行时冻结。如果我一次只运行一个线程,这个问题仍然会发生。

如果有人能指出我是否错误地使用了 SwingWorker 或者是否存在我不知道的其他问题,我将不胜感激。谢谢你的时间。 伊恩

Edit

doCalculation() 只是耗时的事情:

private String doCalculation() {
    for (int i = 0; i < 10000000; i++) {
        Math.pow(3.14, i);
    }
    return threads++ + "";
}

抱歉,即使使用您的 doCalculate() 方法,我仍然无法重现您的问题。例如这是我的sscce http://sscce.org:

import java.awt.event.*;
import java.util.concurrent.ExecutionException;
import javax.swing.*;

public class FooGui {
   private static int threads = 0;

   private static void createAndShowUI() {
      final JLabel label = new JLabel("      ");
      JButton button = new JButton("Button");
      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            new SwingWorker<String, Void>() {

               @Override
               protected String doInBackground() throws Exception {
                  return doCalculation();
               }

               @Override
               protected void done() {
                  try {
                     label.setText(get());
                  } catch (InterruptedException e) {
                     System.out.println("thread was interrupted");
                  } catch (ExecutionException e) {
                     System.out.println("there was an ExecutionException");
                  }
               }
            }.execute();
         }
      });
      JPanel panel = new JPanel();
      panel.add(button);
      panel.add(label);

      JFrame frame = new JFrame("FooGui");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   private static String doCalculation() {
      for (int i = 0; i < 5000000; i++) {
         Math.pow(3.14, i);
      }
      return threads++ + "";
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {

         @Override
         public void run() {
            createAndShowUI();
         }
      });
   }
}

您可能希望创建并发布“简短、独立、正确(可编译)示例”或 SSCCE http://sscce.org您自己的(请检查链接)。我敢打赌,在创建这个的过程中,您可能会自己找到问题及其解决方案。如果是这样,请务必回到这里告诉我们。

我知道这并不是真正的答案,但无法在评论中发布这样的代码。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

即使使用 SwingWorker,Java GUI 也会冻结 的相关文章

随机推荐