简体   繁体   English

用多个线程迭代和修改jtree

[英]iterating and modifying jtree with multiple threads

I have a very large JTree and I would like to navigate through all nodes starting from root to leaves. 我有一个很大的JTree,我想浏览从根到叶的所有节点。

I need to do some kind of regroupping based on user input. 我需要根据用户输入进行某种重新组合。

For example, I want all the nodes whose user object string starts with "a" to be regroupped inside a new node inside the tree. 例如,我希望其用户对象字符串以“ a”开头的所有节点都在树内的新节点内重新分组。

Initial : Root - Ann, John, Andrew 首字母缩写:Root-Ann,John,Andrew

After : Root - A - Ann Andrew J - John 之后:根-A-安德鲁·J-约翰

I was thinking about start iterating and start a bunch of threads for each node and synchronize when creating a new node. 我正在考虑开始迭代并为每个节点启动一堆线程,并在创建新节点时进行同步。

Any thoughts? 有什么想法吗?

A bunch of threads ... unless you mean you are going to construct a new TreeModel in the background by using multiple Thread s and then replace your whole TreeModel of your JTree on the EDT in one go this would be a very bad idea. 一堆线程 ...除非您要使用多个Thread在后台构造一个新的TreeModel ,然后一次性替换EDT上JTree整个TreeModel ,否则这将是一个非常糟糕的主意。

Swing components should only be altered on the EDT, so if you are going to re-order nodes from multiple Thread s directly on the TreeModel that is placed on your JTree you will most likely end up with a corrupt representation of your JTree . 仅应在EDT上更改Swing组件,因此,如果要直接从放置在JTree上的TreeModel上的多个Thread重新排序节点,则很有可能最终会破坏JTree表示形式。

As Robin said, doing it directly on the JTree in multiple threads will cause race conditions. 如Robin所说,直接在多个线程的JTree上执行此操作将导致争用条件。

You could copy your TreeModel and alter it. 您可以复制TreeModel并进行更改。 But be aware that you still have to synchronize between your threads. 但是请注意,您仍然必须在线程之间进行同步。 Afterward you could simply call SwingUtilities.invokeLater and set the copied and altered model as the new model for the JTree. 之后,您可以简单地调用SwingUtilities.invokeLater并将复制和更改的模型设置为JTree的新模型。

Another idea would be to analyze the data multithreaded, create a (threadsafe) collection with the results and alter the model at the EDT with the results. 另一个想法是分析多线程数据,使用结果创建(线程安全)集合,并使用结果在EDT更改模型。 Having a big tree and little modifications, this would be the fastest way (no copy, no full rebuild of the tree) 有一棵大树,几乎没有修改,这将是最快的方法(无副本,无树的完整重建)

Hint: If you are using Java 7, have a look at the Fork/Join-Framework. 提示:如果您使用的是Java 7,请查看Fork / Join-Framework。 If not, do it nonetheless to get the idea. 如果没有,请尽其所能。 Implementation should be easy enough, otherwise there are implementations around. 实现应该足够容易,否则周围会有实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM