我正在尝试使用 Task 更新 preogress 栏。这是我的代码:
ObservableList<somePOJO> listWithProblem = FXCollections.observableArrayList();
Task task = new Task<Void>() {
@Override
public Void call() throws Exception {
final int max = 10;
int i = 0;
while (i < max) {
if (isCancelled()) {
break;
}
if (i == 0) {
i++;
List<SomePOJO> someList = someActionReturningList();
listWithProblem.clear(); // This list has problem!
if (!someList.isEmpty()) {
for (SomePOJO object : someList) {
listWithProblem.add(object);
}
}
Thread.sleep(1000);
updateProgress(i, max);
} else {
i++;
Thread.sleep(1000);
updateProgress(i, max);
}
}
}
return null;
}
};
ProgressBar bar = new ProgressBar(0);
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();
好像每次都卡在了线上listWithProblem.clear();
。如果我删除它,一切都会好起来的。我不明白为什么会这样。感谢您的任何提示!