Java: how to speed up JTree reconstruction by reading data from mongodb (remote server)?
basically mongodb stores each node userobject(NodePro) with parentId.
This class recursively builds a Jtree from querying all the children of a given parentId.
The problem is I have to wait up to 5 m开发者_Go百科inutes for the entire tree to load when dealing with hundreds or thousands of nodes of a single tree.
Is there a way to speed this up significantly? currently even dealing with under hundred nodes, it will take a long time for the tree to become complete.
public class BuildTree {
public BuildTree(DefaultMutableTreeNode treeNode){
DefaultMutableTreeNode aParentNode = treeNode;
try {
processChildren(aParentNode);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void processChildren(DefaultMutableTreeNode parentNode) throws Exception{
NodePro np = (NodePro) parentNode.getUserObject();
List<NodePro> nodelist = NodeDAO.getInstance().getChildrenOfParent(np.getId().toString());
if (nodelist.isEmpty()){
System.out.println("empty");
return;
}else{
for (int i=0; i< nodelist.size(); i++)
{
NodePro childnode = nodelist.get(i);
DefaultMutableTreeNode child = new DefaultMutableTreeNode(childnode);
TreeModel.getInstance().insertNodeInto(child,parentNode,TreeModel.getInstance().getChildCount(parentNode));
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)
TreeModel.getInstance().getChild(parentNode, i);
processChildren(parent);
}
return;
}
}
@trashgod is right in his comment: where is the code spending its time? Is it in the object creation? DefaultMutableTreeNode
doesn't perform well when you've got thousands of node to create. Is it during retrieval of the node information from MongoDB?
If you can't speed up the creation of the tree, you'll want to investigate lazily-loading the children of a node. That is, don't load the node until the user requests to see the children.
Other possibilities are to load the children nodes in a background thread.
However, like any performance improvement work, you need to measure which part of your code is slow and then figure out how to improve it.
that would be comment but
remove enless MongoDb engine
in your topics, that nothing to do with your issue (and for number of your posts)
there are three areas
1/ you are not familair with (Abstract/Default) TreeModel
correct and proper way, create JTree
and its Model
once, not as you displayed, then strictly separeted Swing JComponets
and Bussines Logics (load/change/manage/save/whatever with data for Swing JComponents
), your create new void()
for any of your action
2/ issues with Concurency in Swing, bacause anything from/to database is basically BackGroung Task, anything must be wrappef into invokeLater()
and is is there Custom Java Look and Feel
then into invokeAndWait()
3/ expands as @trashgod and @StanislavL correctly suggested carefully reads this post about unclosed JDBC Objects
精彩评论