ConcurrentModificationExceptionsは、2つのスレッドで同じListにアクセスすると発生します。あなたのTreeModelはおそらくArrayLists、Vectors、Hashtables、またはノードを格納するのに似たものを使用します。
1)あなたのTreeModelのは常にEDTのスレッドでJTreeのことで、それがレンダリングされるたびに照会されます。
私の推測では、2つのいずれかが起こっているということです。これはやむを得ないことであり、Swingの仕組みです。ツリーモデルにアクセスしたり、別のスレッド上の基になるリストにアクセスしている場合は、レンダリングと同時にそのリストを実行し、Listが例外をスローします。
2)2人のスイングワーカーがそれぞれ独自のスレッドで動作しています。彼らは同時にリストを更新/照会しています。上記と同じ問題。
Tomとは、SwingWorkerを使ってTreeModelを「非同期」にすることは非常に難しい問題であり、避けるべきです。しかし、あなたの質問に答えるために、私は次のコードを見ていきます。ツリーに対するすべてのクエリおよび更新アクションが、EDTで常に実行される「完了」メソッドでどのように行われるかに注目してください。私の推測では、construct()メソッドでgets/setsをやっているということです。
public TreeNode[] getChildren(TreeNode parent){
// Do all normal work you would do
...
// figure out if this parents children have not been fetched yet
boolean needToFetch = ....
if(needToFetch){
worker = new SwingWorker() {
public Object construct() {
// go fetch your children from whatever database, server, file...
// fetchNewChildren must NOT access the TreeModel or any of the underlying
// lists, because you will get a modification exception. This is what
// Tom meant by "Pass a set of arguments into the background thread
// that are not being used in the EDT"
return fetchNewChildNodes(parent);
}
public void finished() {
List<TreeNode> newNodes = (List<TreeNode>)get();
// insert the newly fetched nodes into the parent
// if done correclty, this will fire TreeModelChanged events
// so the tree should re-render, repaint, etc...
parent.addChildren(newNodes);
}
};
worker.start();
}
であなたのTreeModel内のデータを格納してみてください?そして/または例外スタックトレース? – harto