Unable to catch ArrayIndexOutOfBoundsException from Future> and SwingWorker if thread starts Executor
Problem:
I am using Executor for SwingWorker incorrectly identifies the wrong elements from a Vector when multi-threaded, it looks like this code is rather ignoring elements that are not present in the Vector.
Problem details:
Problem output:
运行: 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE 构建成功(总时间:11 秒)
Exception raised by uncommenting the following code:
// 更改 TableValues1(); // 取消注释以获取 ArrayIndexOutOfBoundsException
Modified output:
运行: 线程“AWT-EventQueue-0”中出现异常java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:2 在 java.util.Vector.get(Vector.java:694) 在 KondorExport.Util.Help.Table.TableWithExecutor.changeTableValues1(TableWithExecutor.java:70) 在 KondorExport.Util.Help.Table.TableWithExecutor.access0(TableWithExecutor.java:18) 在 KondorExport.Util.Help.Table.TableWithExecutor.actionPerformed(TableWithExecutor.java:61) 在 javax.swing.Timer.fireActionPerformed(Timer.java:271) 在 javax.swing.Timer$DoPostEvent.run(Timer.java:201) 在 java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE 构建成功(总时间:10 秒)
Code snippet:
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.List; import java.util.Vector; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import javax.swing.*; import javax.swing.table.*; public class TableWithExecutor extends JFrame { // ... 代码 ... private void changeTableValues1() { fwDeals1 = new Vector<String>(); fwDeals1.add("First"); // ElementAt(0) fwDeals1.add("Second");// ElementAt(1) checkDealsInDb1(fwDeals1.get(1), fwDeals1.get(2)); } // ... 代码 ... }
Edit:
by in I added the rethrowing of exceptions in the done() method in Future#get() (thanks to @takteek for the great suggestion).
@Override protected void done() { try { get(); // errLabel.setText(String.valueOf(get())); } catch (InterruptedException ie) { ie.printStackTrace(); } catch (ExecutionException ee) { ee.printStackTrace(); } catch (IllegalStateException is) { is.printStackTrace(); } }
But the output is still only:
运行: Got exception 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE Got exception 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE Got exception 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE Got exception 线程状态,名称:StartShedule,SwingWorker 状态为 STARTED 线程状态,名称:StartShedule,SwingWorker 状态为 DONE 构建成功(总时间:13 秒)
The above is the detailed content of Why Can't I Catch an `ArrayIndexOutOfBoundsException` from a `Future` and `SwingWorker` when Using an `Executor`?. For more information, please follow other related articles on the PHP Chinese website!