我需要使用Swingworker导入一批数据,因为用户需要在使用GUI的导入过程中做出决定。如果我只有一批数据要导入,那么没有问题,但是如果我有多批数据,我需要多个Swingworker,并可能对数据库进行并发访问。我尝试在for循环中执行导入,但是它们同时启动全部,从而导致一致性问题。我希望仅在上一个Swingworker完成后才激活它。我不能(轻松地)重写swingworker用于创建同步的功能。
final JFileChooser fc;
if (view.LAST_FILES_POSITION == null) {
fc = new JFileChooser();
} else {
fc = new JFileChooser(new File(view.LAST_FILES_POSITION));
}
view.LAST_FILES_POSITION = fc.getSelectedFile().getParent();
File[] flows = fc.getSelectedFiles();
for(File flow: flows) {
((view)this).activateBusy("Importing Flow '" + flow.getName() + "' to database ...");
try {
ImportFlowTask tftask = new ImportFlowTask(this, flow);
tftask.execute();
((view)this).deactivateBusy();
} catch (DBInconsistencyException ex) {
ex.printStackTrace();
}
}
public class ImportTask extends SwingWorker<Object, Object>{
//Class does some job...
}