Java Swing工作线程在大型XML解析期间导致延迟

Java Swing工作线程在大型XML解析期间导致延迟,java,multithreading,swing,xml-parsing,swingworker,Java,Multithreading,Swing,Xml Parsing,Swingworker,在我的publicstaticmain[]方法中,我使用以下方法创建GUI: SwingUtilities.invokeLater(new Runnable() { public void run() { app = new Main(); } }); 然后从Swing按钮单击我创建Swing Worker线程,使用: LoadFile lf = new LoadFile(); lf.execute(); 其中,execute()命令调用扩展Swi

在我的
publicstaticmain[]
方法中,我使用以下方法创建GUI:

SwingUtilities.invokeLater(new Runnable() 
{
    public void run() 
    {
        app = new Main();
    }
});
然后从Swing按钮单击我创建Swing Worker线程,使用:

LoadFile lf = new LoadFile();
lf.execute();
其中,
execute()
命令调用扩展
SwingWorker
的类的
doInBackground()
方法。该方法不访问该方法之外的资源。它所做的工作是读取一个约227000行、约15列(300MB)的文件。这大约需要12秒

doInBackground()
线程由以下代码组成:

@Override
protected ArrayList<LinkedHashMap<String, String>> doInBackground() throws Exception 
{
Map <String, String> profMap = new LinkedHashMap<String, String>();
profMap.putAll(Settings.xmlMap);

ArrayList<LinkedHashMap<String, String>> tempProfData = new ArrayList<LinkedHashMap<String, String>>();

try {
    File fXmlFile = new File(Settings.datasetFile);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName(profMap.get("Node Name"));

    for (int i = 0; i < nList.getLength(); i++) 
    {
    Node nNode = nList.item(i);

    if (nNode.getNodeType() == Node.ELEMENT_NODE) 
    {
        Element eElement = (Element) nNode;
        int count = 0;

        LinkedHashMap<String, String> tempMap = new LinkedHashMap<String, String>();

        for (String key : profMap.keySet())
        {
            if (count >= 2)
            {
                String value = eElement.getElementsByTagName(profMap.get(key)).item(0).getTextContent();
                tempMap.put(key, value);
            }

            count++;
        }    

        tempProfData.add(tempMap);
    }
    }

} 
catch (Exception e) 
{
    e.printStackTrace();
}

return tempProfData;
}
@覆盖
受保护的ArrayList doInBackground()引发异常
{
Map profMap=新建LinkedHashMap();
profMap.putAll(Settings.xmlMap);
ArrayList tempProfData=新建ArrayList();
试一试{
File fXmlFile=新文件(Settings.datasetFile);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(profMap.get(“节点名称”));
对于(int i=0;i=2)
{
String value=eeelement.getElementsByTagName(profMap.get(key)).item(0.getTextContent();
tempMap.put(键、值);
}
计数++;
}    
添加(tempMap);
}
}
} 
捕获(例外e)
{
e、 printStackTrace();
}
返回数据;
}
当这发生时,我在屏幕上有一个正在加载的动画,但当它发生时,它可能会变得有点滞后。任务管理器中的应用程序的CPU似乎在100%运行


这么大的任务会减慢应用程序的速度,还是我遇到了麻烦?GUI不会完全冻结,但如果您单击某个对象,它有时会明显延迟约半秒,直到线程完成

为了更好的帮助,发布一个或部分问题可能是TimePrimDATA的连续调整大小,可能考虑提供更大的初始值。不要忘记临时变量可以添加gcoverhead@MadProgrammer我将数组的大小设置为数据集的大小,但这并不影响时间,节省了一秒钟,但GUI仍然滞后。发布or,以便更快地获得更好的帮助。部分问题可能是tempProfData的不断调整大小,也许考虑提供更大的初始值。不要忘记临时变量可以添加gcoverhead@MadProgrammer我将数组的大小设置为数据集的大小,但这并不影响时间,节省了一秒钟,但GUI仍然滞后