gpt4 book ai didi

java - SwingWorker 完成后挂起 : Is it bad practise to start and run a thread pool from a SwingWorker?

转载 作者:行者123 更新时间:2023-11-30 03:49:22 28 4
gpt4 key购买 nike

假设一个应用程序读取数据文件,从用户获取一些输入(通过 GUI),从大型数据库获取附加数据并执行冗长的分析。

按照建议,我将耗时的部分(数据库管理和实际分析)删除到了 SwingWorker 实例中。现在执行数据库管理的第一部分工作正常(参见下面的代码):

dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
final File dbFile = dialog.getFile();

frame.activateDisGlass("Loading DB, please wait...");
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
try {
dbman = new DbManager(dbFile);
} catch (SQLException e1) {
System.err.println("Unable to load the chosen database, see details below:");
e1.printStackTrace();
}
return null;
}

@Override
protected void done(){
frame.deactivateDisGlass();
}
};
worker.execute();
}
});

但是,相同的策略在分析案例中无法正常工作。由于分析部分相当长,并且涉及到很多自定义类,我将总结一下情况。以下代码位于一个自定义类中,该类与 GUI 交互以获取用户输入,计算由 analyzeButton 触发,该对象启动多个对象的创建,其中一个对象包含一个线程池并行进行独立分析:

// INITIATE ANALYSIS
analyzeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {

for (int i = 0; i < annots.length; i++) {
annots[i] = (ANNOT_TYPE) combos[i].getSelectedItem();
}

// Validate the column annotations
List<ANNOT_TYPE> annotlist = Arrays.asList(annots);
if (!annotlist.contains(ANNOT_TYPE.R) &&
!(annotlist.contains(ANNOT_TYPE.OS1) &&
annotlist.contains(ANNOT_TYPE.OS2))) {
JOptionPane
.showMessageDialog(
null,
"<html>Missing annotation!<p>"
+ "<p> Please make sure you have annotated the datafile correctly...",
"Missing annotation!",
JOptionPane.ERROR_MESSAGE);
return;
}

// Validate DB selection
if (descPanel.getDBManager() == null) {
JOptionPane
.showMessageDialog(
null,
"<html>No database not selected for analysis! <p>"
+ "<p> Please select and load a database..",
"Database error", JOptionPane.ERROR_MESSAGE);
return;
}

// Activate progress indicator
frame.getMainFrame().activateInfiGlass();

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() {
try {
// register parameters
param.addParam(AnalysisParams.value_key,descPanel.getValueTypeComboIndex());
param.addParam(AnalysisParams.sepchar_key,descPanel.getSepCharComboIndex());
paramPanel.registerParams();

StringBuilder sb = new StringBuilder("Data preview completed, initiating analysis...");
sb.append(System.lineSeparator())
.append("... column annotations: ")
.append(Arrays.toString(annots));
logger.info(sb.toString() + System.lineSeparator());

// Create dataset; to be passed on to SwingWorker which will
// execute the analysis
ds = new Dataset();

for (String[] line : data)
ds.addRow(line, annots);

System.out.println("Dataset parsed...");
logger.info("Dataset parsing complete "
+ System.lineSeparator()
+ ds.toString()
+ System.lineSeparator());

conserv = new ConcurrencyService(ds, descPanel.getDBManager());
conserv.serve();
DebugToolbox.dumpScores();
}
} catch (InterruptedException e) {
logger.severe("Concurrency service interrupted"
+ System.lineSeparator()
+ e.getStackTrace()
+ System.lineSeparator());
System.err.println("Interrupt exception!!");
}
return null;
}

@Override
protected void done() {
if(!conserv.isDone())
logger.warning("Concurrency Service is not done!" +
System.lineSeparator());

logger.info("#DEBUG: Conserv should have been terminated by now..." + System.lineSeparator());
frame.getMainFrame().deactivateInfiGlass();
}
};

worker.execute();

}
});

那么问题是什么,您可能会问...问题是 done() 从未在 SwingWorker 上调用,并且应用程序实际上只是挂起,等待某物。我可以看到 doInBackground() 已完成,因为分数已转储到日志文件中(这是该方法中的最后一个操作,但我没有看到 done 的预期输出()

我从 jvisualvm 获得了线程转储,这是相关的部分:

"SwingWorker-pool-1-thread-2" daemon prio=5 tid=0x00007f8ce5819000 nid=0x860b waiting on condition [0x0000000129593000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000705a2c978> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

Locked ownable synchronizers:
- None

前一个 SwingWorker (加载数据库的那个)具有完全相同的状态和堆栈跟踪,这让我认为 SwingWorker 实际上已经完成了它的工作,但应用程序的其余部分不知何故没有收到通知。

类似问题:我注意到两个相关的问题有本质区别

  1. JDK-7 SwingWorker deadlocks? - 我的 SwingWorker 是(或应该)彼此独立的。

  2. SwingWorker hangs at Unsafe.park() - 我的 SwingWorker 不与 GUI 交互或返回任何内容(null 除外),因此它不应该与 get() 有任何关系,对吗?

想法?

最佳答案

您声明:

so it should not have anything to do with get(), right?

并非如此。调用 get() 是关键,否则您将无法捕获和处理 SwingWorker 的 doInBackground 方法中抛出的任何异常。因此,我建议您不要忘记在 done() 方法中对 SwingWorker 调用 get(),也许在方法中首先调用它,然后不要不要忘记捕获并分析此方法引发的任何和所有异常。

关于java - SwingWorker 完成后挂起 : Is it bad practise to start and run a thread pool from a SwingWorker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24805366/

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