我想用一个线程读取一个XML文件,并通过2个线程向其中添加一些节点。 写入器线程不能同时写入文件。读取器线程读取文件内容并将其放入缓冲区。当读取器执行此操作时,写入器无法访问该缓冲区。
我该怎么做?
我读取 XML 文件并通过以下方式向其中添加节点:
public synchronized void AddNewNode(Document doc) throws TransformerException {
Element book = doc.createElement("book");
book.setAttribute("id", "3");
Element name = doc.createElement("name");
name.setTextContent("new");
Element id = doc.createElement("id");
id.setTextContent("31");
Element writer = doc.createElement("writer");
writer.setTextContent("tester");
Element price = doc.createElement("price");
price.setTextContent("3100");
book.appendChild(name);
book.appendChild(id);
book.appendChild(writer);
book.appendChild(price);
doc.getDocumentElement().appendChild(book);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult("/home/zahra/workspace/test1/src/xmlfile.xml");
transformer.transform(source, result);
}
public void ReadNodes(Document doc) {
NodeList nList = doc.getElementsByTagName("book");
System.out.println("\n" + ":0 :) :0 :) :0 :) :0 :) :0 :)");
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
System.out.println("\n" + "Element number " + (i + 1) + "\n");
Element eElement = (Element) nNode;
for (int j = 0; j < eElement.getChildNodes().getLength(); j++) {
String tagName = eElement.getChildNodes().item(j).getNodeName();
if (eElement.getElementsByTagName(tagName).getLength() > 0) {
System.out.println(eElement.getChildNodes().item(j).getNodeName()
+ " : "
+ eElement.getElementsByTagName(tagName).item(0).getTextContent());
}
}
}
}
谢谢!
最佳答案
要非常小心地使用 synchronized
作为方法修饰符;它通常不会达到您的预期。我喜欢将 synchronized
方法修饰符视为以下语法糖:
public void myMethod() {
synchronized(this) {
// method body
}
}
由于它锁定实例本身,因此它仅限制同一实例同时调用该方法。例如,如果您为每个编写器创建一个实例,则同步方法根本没有帮助。在您的情况下,可能更干净的是直接同步您想要锁定的对象,例如:
public void addNewNode(Document doc) throws TransformerException {
synchronized(doc) {
Element book = doc.createElement("book");
book.setAttribute("id", "3");
// etc, etc.
}
}
您也许可以在 Reader 方法中使用相同的技巧,但在这种情况下,读者也会阻止两个作者。我不太清楚您的用例需要这样的线程和计时。即使你让多线程完美工作,你仍然会遇到这样的问题:你的读取实际上是否覆盖了每次写入(可能存在一种竞争情况,即写入发生在你最近一次读取之前)。
关于java - java中通过多线程读写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15403416/