【发布时间】:2014-11-26 11:33:07
【问题描述】:
我有一个与某些数据相关联的 TableView,一旦我按下运行按钮,我就会对该数据执行一些处理。每行数据都在一个单独的线程中处理,当这些线程正在运行时,我想要一个 ProgressInducator 来替换其 vbox 中的表。
在附件代码中:
如果我停在哪里说“如果在这里停止工作” - 表被替换为 pi。 如果我继续等待线程加入 - 无需替换。 我错过了什么?
runButton.setOnAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent e) {
List<Thread> threadList = new ArrayList<Thread>();
int threadCounter = 0;
final ProgressIndicator pi = new ProgressIndicator(threadCounter);
vbox.getChildren().clear();
vbox.getChildren().addAll(pi);
for (ProductInTable product : data) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try
{
product.calculate();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
threadList.add(thread);
thread.start();
}
int x = threadList.size();
/** WORKS IF STOP HERE **/
// wait for all threads to end
for (Thread t : threadList) {
try {
t.join();
threadCounter++;
pi.setProgress(threadCounter / x);
} catch (InterruptedException interE) {
interE.printStackTrace();
}
}
/** DOESNT WORKS IF STOP HERE **/
【问题讨论】:
标签: multithreading javafx tableview progress-indicator