我編寫了一個多線程程序,每個線程都需要解析一個xml文件並更新新的值。問題:假設我有一個xml內容的ABC,現在線程A解析xml並更新它成爲ABCA,同時線程B也解析xml(內容是ABC)並更新它成爲ABCB。線程B在線程A更新後更新xml,所以xml的結果是ABCB,我想要的是xml結果應該是ABCAB。任何想法來控制線程中解析和更新的方式? 這裏是我的代碼:Java解析並更新線程中的xml值
WSthread.java
public class WSthread extends Thread {
public String WSname;
Process proc= null;
WebServicesXML xml;
WSthread(String name){
WSname=name;
}
public void run() {
try {
//my code
// Run a java app in a separate system process
String cmd = (WSname);
proc = Runtime.getRuntime().exec("java -jar "+cmd);
xml = new WebServicesXML();
//Process proc = Runtime.getRuntime().exec("java -jar .jar");
// Then retreive the process output
//InputStream in = proc.getInputStream();
//InputStream err = proc.getErrorStream();
BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
String regex = "\\bhttp\\b";
Pattern pattern = Pattern.compile(regex);
String WSaddress = "";
while ((line = is.readLine()) != null){
Matcher matcher = pattern.matcher(line);
if(matcher.find()){
WSaddress = line;
System.out.println("Updating WS address..."+WSaddress);
xml.create(WSname, WSaddress);
}
System.out.println(line);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void close(){
proc.destroy();
}
WebServicesXML.java
public class WebServicesXML{
public int totalWebServices;
public String WSnamelist[];
public synchronized void create(String WSname, String WSaddress) throws OException, TransformerConfigurationException, TransformerException, ParserConfigurationException{
try {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document readdoc = docBuilder.parse("webservices.xml");
// normalize text representation
readdoc.getDocumentElement().normalize();
Node rootList = readdoc.getDocumentElement();
Element rootElement = (Element)rootList;
Element webservice = readdoc.createElement("WebService");
Element name = readdoc.createElement("name");
Element address = readdoc.createElement("address");
name.setTextContent(WSname);
address.setTextContent(WSaddress);
webservice.appendChild(name);
webservice.appendChild(address);
rootElement.appendChild(webservice);
/////////////////
//Output the XML
//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from xml tree
File file = new File("webservices.xml");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(readdoc);
trans.transform(source, result);
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
謝謝。我的程序現在使用同步工作 – winsontan520 2009-09-08 10:37:07