I play with multitreading for SwingWorker by using Executor, and I'm there by mistake identified wrong elements from the Vector, looks like as this code pretty ignores that element in Vector doesn't exist
my question -> how to/is possible to catch this exception(s) some way
simple output
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)
by uncomment
//changeTableValues1(); // un-comment for get ArrayIndexOutOfBoundsException
everything is correct, I get ArrayIndexOutOfBoundsException and output is
run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
at java.util.Vector.get(Vector.java:694)
at KondorExport.Util.Help.Table.TableWithExecutor.changeTableValues1(TableWithExecutor.java:70)
at KondorExport.Util.Help.Table.TableWithExecutor.access$100(TableWithExecutor.java:18)
at KondorExport.Util.Help.Table.TableWithExecutor$2.actionPerformed(TableWithExecutor.java:61)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
BUILD SUCCESSFUL (total time: 10 seconds)
from code
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 static final long serialVersionUID = 1L;
private String[] columnNames = {"Narrative", "Description"};
private Object[][] data = {{"About", "About"}, {"Add", "Add"}};
private JTable table;
private Executor executor = Executors.newCachedThreadPool();
private Timer timerRun;
private int delay = 3000;
private Vector<String> fwDeals;
private Vector<String> fwDeals1;
public TableWithExecutor() {
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
prepareStartShedule();
}
private void prepareStartShedule() {
timerRun = new javax.swing.Timer(delay, startCycle());
timerRun.setRepeats(true);
timerRun.start();
}
private Action startCycle() {
return new AbstractAction("Start Shedule") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
executor.execute(new TableWithExecutor.MyTask("StartShedule")); //non on EDT
//changeTableValues1(); // un-comment for get ArrayIndexOutOfBoundsException
}
};
}
private void changeTableValues1() {
fwDeals1 = new Vector<String>();
fwDeals1.add("First"); // ElementAt(0)
fwDeals1.add("Second");// ElementAt(1)
checkDealsInDb1(fwDeals1.get(1), fwDeals1.get(2));
}
private void checkDealsInDb1(String Str, String Str1) {
table.getModel().setValueAt(Str, 0, 1);
table.getModel().setValueAt(Str1, 1, 1);
}
private void changeTableValues() {
fwDeals = new Vector<String>();
fwDeals.add("First"); // ElementAt(0)
fwDeals.add("Second");// ElementAt(1)
checkDealsInDb(fwDeals.get(1), fwDeals.get(2));
}
private void checkDealsInDb(String Str, String Str1) {
table.getModel().setValueAt(Str, 0, 1);
table.getModel().setValueAt(Str1, 1, 1);
}
public static void main(String[] args) {
TableWithExecutor frame = new TableWithExecutor();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocation(150, 150);
frame.pack();
frame.setVisible(true);
}
private class MyTask extends SwingWorker<Void, Integer> {
private String str;
private String namePr;
MyTask(String str) {
this.str = str;
addPropertyChangeListener(new SwingWorkerCompletionWaiter(str, namePr));
}
@Override
protected Void doInBackground() throws Exception {
if (str.equals("StartShedule")) {
changeTableValues();
}
return null;
}
@Override
protected void process(List<Integer> progress) {
}
@Override
protected void done() {
if (str.equals("StartShedule")) {
}
}
}
private class SwingWorkerCompletionWaiter implements PropertyChangeListener {
private String str;
private String namePr;
SwingWorkerCompletionWaiter(String str, String namePr) {
this.str = str;
this.namePr = namePr;
}
SwingWorkerCompletionWaiter(String namePr) {
this.namePr = namePr;
}
@Override
public void propertyChange(PropertyChangeEvent event) {
if ("state".equals(event.getPropertyName()) && SwingWorker.StateValue.DONE == event.getNewValue()) {
System.out.println("Thread Status with Name :" + str + ", SwingWorker Status is " + event.getNewValue());
} else if ("state".equals(event.getPropertyName()) && SwingWorker.StateValue.PENDING == event.getNewValue()) {
System.out.println("Thread Status with Mame :" + str + ", SwingWorker Status is " + event.getNewValue());
} else if ("state".equals(event.getPropertyName()) && SwingWorker.StateValue.STARTED == event.getNewValue()) {
System.out.println("Thread Status with Name :" + str + ", SwingWorker Status is " + event.getNewValue());
} else {
System.out.println("SomeThing Wrong happends with Thread Status with Name :" + str);
}
}
}
}
EDIT:
added re-thrown from Future#get() in done() method (by @takteek excellent suggestion)
@Override
protected void done() {
if (str.equals("StartShedule")) {
try {
get();
//errLabel.setText(String.valueOf(get()));
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (ExecutionException ee) {
ee.printStackTrace();
}catch (IllegalStateException is) {
is.printStackTrace();
}
}
}
but output is still and only Got exception
, would be so hard to get any exception(s) from this BlackBox
run:
Got exception
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Got exception
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Got exception
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Got exception
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
BUILD SUCCESSFUL (total time: 13 seconds)
Question&Answers:
os