java Swing工作线程在大型XML解析期间导致延迟
在我的public static main[]
方法中,我使用以下方法创建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;
}
当这发生时,我在屏幕上有一个正在加载的动画,但当它发生时,它可能会变得有点滞后。任务管理器中的应用程序的CPU似乎在100%运行
这么大的任务会减慢应用程序的速度,还是我遇到了麻烦?GUI不会完全冻结,但如果您单击某个对象,它有时会明显延迟约半秒,直到线程完成
共 (0) 个答案