我在Java 6中使用SwingWorker来避免在事件派发线程上运行长时间运行的代码.
如果在我的done()方法中调用get()会返回异常,那么处理异常的适当方法是什么?
我特别关注可能的InterruptedExceptions.JavaDoc示例简单地忽略了异常,但多年来我已经了解到吞咽异常导致难以调试的代码.
示例用法如下:
new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
// do long-running calculation
return result;
}
@Override
protected void done() {
try {
setTextField(get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}.execute();
Run Code Online (Sandbox Code Playgroud) 我通过使用Executor玩SwingWorker的多线程,我在那里错误地从Vector中识别出错误的元素,看起来像这个代码相当忽略了Vector中的元素不存在
我的问题 - >如何/有可能以某种方式捕获此异常
简单的输出
run:
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
BUILD SUCCESSFUL (total time: 11 seconds)
Run Code Online (Sandbox Code Playgroud)
通过取消注释
//changeTableValues1(); // un-comment for get ArrayIndexOutOfBoundsException
Run Code Online (Sandbox Code Playgroud)
一切都正确,我得到ArrayIndexOutOfBoundsException并输出
run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: …Run Code Online (Sandbox Code Playgroud) 我已经和SwingWorker一起工作了一段时间并且最终出现了一种奇怪的行为,至少对我而言.我清楚地了解到,由于性能原因,一次调用中对publish()方法的几次调用是有用的.这对我来说非常有意义,我怀疑SwingWorker会保留某种队列来处理所有调用.
根据教程和API,当SwingWorker结束执行时,doInBackground()正常完成或工作线程从外部取消,然后调用done()方法.到现在为止还挺好.
但是我有一个示例(类似于教程中所示),其中在执行process()方法之后 done()执行了方法调用.由于两个方法都在Event Dispatch Thread中执行,因此我希望done()在所有process()调用完成后执行.换一种说法:
Writing...
Writing...
Stopped!
Run Code Online (Sandbox Code Playgroud)
Writing...
Stopped!
Writing...
Run Code Online (Sandbox Code Playgroud)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Demo {
private SwingWorker<Void, String> worker;
private JTextArea textArea;
private Action startAction, stopAction;
private void …Run Code Online (Sandbox Code Playgroud) 我在应用程序中显示等待光标时遇到问题.只要鼠标位于定义自己光标的面板上方,就不会出现等待光标.如果面板未更改光标,则会出现等待光标.
我附上了SSCE来准确解释我的问题.
public class BusyCursorTest extends javax.swing.JFrame {
public BusyCursorTest() {
javax.swing.JMenuBar menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu menu = new javax.swing.JMenu("Menu");
javax.swing.JMenuItem wait1 = new javax.swing.JMenuItem("Wait 100 ms");
javax.swing.JMenuItem wait2 = new javax.swing.JMenuItem("Wait 250 ms");
javax.swing.JMenuItem wait3 = new javax.swing.JMenuItem("Wait 500 ms");
javax.swing.JMenuItem wait4 = new javax.swing.JMenuItem("Wait 1000 ms");
menu.add(wait1);
menu.add(wait2);
menu.add(wait3);
menu.add(wait4);
menuBar.add(menu);
setJMenuBar(menuBar);
wait1.addActionListener(getActionListener(this, delayActionListener(100)));
wait2.addActionListener(getActionListener(this, delayActionListener(250)));
wait3.addActionListener(getActionListener(this, delayActionListener(500)));
wait4.addActionListener(getActionListener(this, delayActionListener(1000)));
cursorPanel = new javax.swing.JPanel();
cursorPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent e) {
cursorPanel.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.CROSSHAIR_CURSOR));
}
public void mouseExited(java.awt.event.MouseEvent …Run Code Online (Sandbox Code Playgroud) 说我有以下代码:
import java.lang.InterruptedException;
import javax.swing.SwingWorker;
public class Test
{
private JDialog window;
public Test
{
// instantiate window
}
private class Task extends SwingWorker<Void, Void>
{
public Void doInBackground()
{
try { Thread.currentThread().sleep(5000); }
catch(InterruptedException e) {}
return null;
}
}
public void doTask()
{
Task task = new Task();
task.execute();
}
protected void process()
{
// update various GUI components here
}
public static void main(String args[])
{
Test t = new Test();
t.doTask();
System.out.println("done");
}
}
Run Code Online (Sandbox Code Playgroud)
我需要等到t.doTask() …
我有两个SwingWorker类:FileLineCounterThread和FileDivisionThread
我将执行两个线程.当计数线程完成时,它会将结果传递给File Division线程.
我不知道如何将结果传递给已启动的线程.
目前我有两个SwingWorker线程在后台工作.如果发生异常,该方法将停止工作,但该线程仍然运行.
如何停止执行并杀死doInBackground()发生异常的线程?
this.cancel(true)不要破坏/关闭线程.我怎样才能做到这一点?
@Override
protected Boolean doInBackground() throws Exception {
try {
while (true) {
//some code here
return true;
}
} catch (Exception e) {
this.cancel(true); //<-- this not cancel the thread
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我在Netbeans的调试中看到了这些线程.
'AWT-EventQueue-0' em execução
'AWT-Windows' em execução
'SwingWorker-pool-1-thread-1' em execução
'SwingWorker-pool-1-thread-2' em execução
//*em execução = in execution
Run Code Online (Sandbox Code Playgroud)