gpt4 book ai didi

java - 在后台更新 JavaFX Tree 时出现 ConcurrentModificationException

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:14 26 4
gpt4 key购买 nike

我的代码创建了 TreeItem<String>在后台任务中,因为我有很多它们并且它们的创建需要相当长的时间,应用程序会卡住。在这个例子中它没有多大意义,但它说明了我在实际应用程序中遇到的问题。扩展节点时,程序会抛出 ConcurrentModificationException。

我使用 jdk1.7.0_17 和 JavaFX 2.2.7

有谁知道如何创建线程安全的 Tree或者如何规避这个问题?

异常

java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at com.sun.javafx.collections.ObservableListWrapper$ObservableListIterator.next(ObservableListWrapper.java:681)
at javafx.scene.control.TreeItem.updateExpandedDescendentCount(TreeItem.java:788)
...

代码

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.security.SecureRandom;
import java.util.Random;


public class ConcurrentExample extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) throws Exception {
TreeView<String> treeView = new TreeView<String>(createNode("root"));
HBox hBox = new HBox();
hBox.getChildren().addAll(treeView);
Scene scene = new Scene(hBox);
stage.setScene(scene);
stage.show();
}

Random r = new SecureRandom();

public TreeItem<String> createNode(final String b) {
return new TreeItem<String>(b) {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;

@Override
public ObservableList<TreeItem<String>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
buildChildren(super.getChildren());
}
return super.getChildren();
}

@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
isLeaf = r.nextBoolean() && r.nextBoolean() && r.nextBoolean();
}
return isLeaf;
}

private void buildChildren(final ObservableList<TreeItem<String>> children) {
if (!this.isLeaf()) {
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
int i;
int max = r.nextInt(500);
for (i = 0; i <= max; i++) {
children.addAll(new TreeItem[]{createNode("#" + r.nextInt())});
}
return i;
}
};
new Thread(task).start();
}
}
};
}

}

最佳答案

目前的答案没有帮助。关键是你必须在 Platform.runLater 的帮助下在主线程中执行子线程的更新

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.security.SecureRandom;
import java.util.Random;


public class Example extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) throws Exception {
TreeView<String> treeView = new TreeView<String>(createNode("root"));
HBox hBox = new HBox();
hBox.getChildren().addAll(treeView);
Scene scene = new Scene(hBox);
stage.setScene(scene);
stage.show();
}

Random r = new SecureRandom();

public TreeItem<String> createNode(final String b) {
return new TreeItem<String>(b) {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;

@Override
public ObservableList<TreeItem<String>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
buildChildren(super.getChildren());
}
return super.getChildren();
}

@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
isLeaf = r.nextBoolean() && r.nextBoolean() && r.nextBoolean();
}
return isLeaf;
}

private void buildChildren(final ObservableList<TreeItem<String>> children) {
if (!this.isLeaf()) {
Platform.runLater(new Runnable() {
@Override
public void run() {
int i;
int max = r.nextInt(500);
for (i = 0; i <= max; i++) {
children.addAll(new TreeItem[]{createNode("#" + r.nextInt())});
}
}
});
}
}
};
}
}

这家伙遇到了同样的问题:http://blog.idrsolutions.com/2012/12/handling-threads-concurrency-in-javafx/

关于java - 在后台更新 JavaFX Tree 时出现 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16125311/

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