gpt4 book ai didi

java - SwingWorker:取消工作线程后调用done()时抛出错误

转载 作者:行者123 更新时间:2023-12-02 12:17:18 26 4
gpt4 key购买 nike

取消 SwingWorke 任务后,我看到以下异常。

Exception in thread "AWT-EventQueue-0" java.util.concurrent.CancellationException

在网上搜索后我发现below comment from Oracle

Note: If get is invoked on a SwingWorker object after its background task has been cancelled, java.util.concurrent.CancellationException is thrown.

这是我生成异常的代码。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class JasperReportGenerator {

private JButton cancel;
private JProgressBar pbar;
private int count = 0;
private JFrame dialog;

public JasperReportGenerator() {
init();
//Here we do the background task
SwingWorker<Boolean, Integer> worker = new SwingWorker<Boolean, Integer>() {
@Override
protected Boolean doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
publish(i * 10);
Thread.sleep(1000);
}
return true;
}

@Override
protected void done() {
try {
dialog.dispose();
JOptionPane.showMessageDialog(null, "Done", get().toString(), JOptionPane.INFORMATION_MESSAGE);
} catch (InterruptedException ex) {
Logger.getLogger(JasperReportGenerator.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(JasperReportGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
protected void process(List<Integer> list) {
pbar.setValue(list.get(list.size() - 1));
pbar.setString(list.get(list.size() - 1) + "%");
}
};
worker.execute();

//cancel the process
cancel.addActionListener((ActionEvent e) -> {
worker.cancel(true);
dialog.dispose();
System.out.println("bg task cancelled...");
});

}

private void init() {
dialog = new JFrame("Progress Report");
dialog.setLocationRelativeTo(null);
cancel = new JButton("Cancel");
pbar = new JProgressBar(0, 100);
pbar.setStringPainted(true);
pbar.setIndeterminate(false);
dialog.setPreferredSize(new Dimension(400, 250));
dialog.setLayout(new FlowLayout());
dialog.getContentPane().add(pbar);
dialog.getContentPane().add(cancel);
dialog.pack();
dialog.setVisible(true);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new JasperReportGenerator();
});
}

}

我搜索了我看到的解决方案 thisthis但我无法将它们放入我的案例中。

这是我在 done() 方法中为解决问题所做的事情

  @Override
protected void done() {
try {
if (!isCancelled()) {
JOptionPane.showMessageDialog(null, "Done", get().toString(), JOptionPane.INFORMATION_MESSAGE);
dialog.dispose();
}

} catch (InterruptedException ex) {
Logger.getLogger(JasperReportGenerator.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(JasperReportGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}

按照上面的设计,我的 done() 不会抛出异常。

现在我想知道这是否是一个好的解决方案?这段代码有什么含义?

最佳答案

您的解决方案并不完美,因为在调用 isCancelled() 和调用 get() 之间可能会发生取消,这很少会导致异常。更好的方法是捕获 CancellationException 并处理它。

关于java - SwingWorker:取消工作线程后调用done()时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46076092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com