作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对多线程了解不多,但我认为它可能对我正在做的事情有用。我有一些东西可以循环遍历 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/
嘿。本周的一个教程,其中一个问题要求通过使用其他函数 formatLine 和 formatList 创建一个函数 formatLines,以格式化行列表。 我的代码是这样的; type Line =
我是一名优秀的程序员,十分优秀!