0

i want to read an XML file with one thread and add some nodes to it through 2 threads. writer threads can not write to file concurrently.and reader thread read file content and put them into a buffer.when the reader do this operation, writers can not access to this buffer.

how can i do this??

i read XML file and add nodes to this in this way:

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());
            }
        }

    }

}

thanks!

1
  • Please restate your question in standard English. The part starting " the writer threads can't access to file" makes no sense whatsoever. Possibly this shows that you haven't thought out your problem fully. Commented Mar 14, 2013 at 7:36

1 Answer 1

1

Be very careful how you use synchronized as a method modifier; it often does not do what you'd expect. I like to think of the synchronized method modifier as syntactic sugar for:

public void myMethod() {
    synchronized(this) {
        // method body
    }
}

Since it locks on the instance itself, it only restricts the same instance from calling the method at the same time. For example, if you created one instance for each writer, the synchronized method will not help at all. What might be cleaner in your case is to directly synchronize on the object that you want locked, like:

public void addNewNode(Document doc) throws TransformerException {

    synchronized(doc) {
        Element book = doc.createElement("book");
        book.setAttribute("id", "3");
        // etc, etc.
    }
}

You might be able to get away with the same trick in your Reader method, however in that case the reader would also be blocking out both writers. I'm not exactly clear on your use case which would require such threading and timing though. Even if you got the multi-threading working perfectly, you'd still have the issue of whether or not your read did in fact cover every write (there could be a race case where the write occurred just before your most recent read).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.