gpt4 book ai didi

java - 多线程-为每个循环创建新线程

转载 作者:行者123 更新时间:2023-12-02 04:34:14 25 4
gpt4 key购买 nike

我对多线程了解不多,但我认为它可能对我正在做的事情有用。我有一些东西可以循环遍历 xml 文件中的每个图形节点,并根据读取的信息创建一个对象。根据对象,它可以接受一个或多个查询,或者将使用存储过程。然后该对象存储在 arrayList 中。最终我的程序需要大约一分钟才能启动。有没有一种方法可以在每次循环时创建一个新线程?这是我的代码:

            File fXmlFile = f;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

//Normalize the xml file(document)
doc.getDocumentElement().normalize();

// All of the nodelists that the XML file uses
NodeList graphicNodeList = doc.getElementsByTagName("graphic");
NodeList locationNodeList = doc.getElementsByTagName("location");
NodeList gUINodeList = doc.getElementsByTagName("GUI");
NodeList storedProcedureNodeList = doc.getElementsByTagName("storedProcedure");
NodeList parameterNodeList = doc.getElementsByTagName("parameter");
NodeList dialSpecificNodeList = doc.getElementsByTagName("dialSpecific");
NodeList queriesNodeList=doc.getElementsByTagName("queries");
NodeList tickerSpecificNodeList=doc.getElementsByTagName("tickerSpecific");
doc.getElementsByTagName("category");


// Main loop to get information from each graphical Element.
for (int temp = 0; temp < graphicNodeList.getLength(); temp++) {
// Sets the node to the first item in the nodeList
Node graphicNode = graphicNodeList.item(temp);


String chartType;

if (graphicNode.getNodeType() == Node.ELEMENT_NODE) {
// Makes an element based off the node.
Element eElement = (Element) graphicNode;

//Get the Chart type so the system knows what to do with it.
chartType= eElement.getAttribute("type");

if(chartType.equals("barChartVsTime")){
createBarChartVsTime(graphicNode, gUINodeList,locationNodeList, storedProcedureNodeList, parameterNodeList, eElement);
}
if(chartType.equals("sqlTable")){
createSQLTable(graphicNode, gUINodeList, eElement);
}
if(chartType.equals("dialChart")){
createDialChart(graphicNode, gUINodeList, dialSpecificNodeList, eElement);
}
if(chartType.equals("hourlyDialChart")){
createHourlyDialChart(graphicNode, gUINodeList, dialSpecificNodeList, eElement);
}
if(chartType.equals("colorBlock")){
createColorBlock(graphicNode, gUINodeList, eElement);
}
if(chartType.equals("image")){
createImageBlock(graphicNode, gUINodeList, eElement);
}
if(chartType.equals("threePointAverageChart")){
createThreePointAverageChart(graphicNode, gUINodeList,locationNodeList, storedProcedureNodeList, parameterNodeList, eElement);
}
if(chartType.equals("yieldDialChart")){
createYieldDialChart(graphicNode, gUINodeList, locationNodeList, storedProcedureNodeList, dialSpecificNodeList, parameterNodeList, eElement);
}

if(chartType.equals("ticker")){
createTicker(graphicNode, gUINodeList,tickerSpecificNodeList, queriesNodeList, eElement);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

I don't know that much about multi threading but I think that it may be of use to what I am doing.

This ends up with my program taking about a minute to start

你有点超前了。您应该首先分析您的代码,看看哪里会影响性能。请查看 VisualVM 来了解这一点。您可能不小心编写了 O(n^2) 算法。

Depending on the object it can take in a querie(s) or it will use a stored procedure.

我打赌它就在这里。连接池或批处理请求可能比立即投入线程更好。

关于java - 多线程-为每个循环创建新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31033191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com