The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Building DOM object from multiple threads

My multi threaded application processes and loads records into an ECM repository. For reconcliation purposes , I am trying to build an XML format report with the results of the processing per record, while processing is underway per thread. The method below is called from every thread wvery time its ready to append an element to the DOM with the status.

The problem is that when under load, the threads just seem to hang while the transformer.transform(source, result) call keeps getting executed until there is an interrupt of some sort. I was able to examine a section of what was appended and it was status for records that had finished processing very early in the process based on my application logs. Once an interrupt is recieved , it looks like the threads recover.

Any idea whats wrong in here ? Thanks in advance for your advice - I have been stuck trying to figure out where the problem out for days! Now that I know where the problem is , I cant figure out a solution. Looking for multi threading gurus..

Re: Building DOM object from multiple threads

A little bit of Q&A:
Why are you synchronizing the XMLReportHandler class?
Do you fully understand what the synchronized keyword does? (just making sure this was your intention)

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: Building DOM object from multiple threads

I am synchronizing the XMLReportHandler because multiple threads are writing to the same object in memory in the writeReportLine method. Also multiple threads are writing to the same file object on the file system in the writeReportFile method. My understandding is that synchronizing will help make sure that the access to the I al ts is exclusively availble to one thread at a time.
I alos chose to use synchronized because I have a text format report in the same application and it executes this code - works perfectly:

Re: Building DOM object from multiple threads

To my understanding of synchronization in java, any thread which attempts to access a synchronized block of code will get put into a "holding pattern" (of sorts) until any other threads have cleared the block.
For synchronized statements (which is what you are using), the lock is applied to the object inside the parentheses, and released when the synchronized block finishes. Every other thread which attempts to access the locked object will get placed in the "holding pattern" until the lock is released (even if the other threads are trying to access the object without a synchronized block) and it can continue.

I'm not sure if it will matter, but: are they writing to the same instance of XMLReportHandler? I ask because:

// Synchronizes ALL instances across all threads of the XMLReportHandler classsynchronized(XMLReportHandler.class){...}// Synchronizes just the reportOutput instance of the XMLReportHandler class across all threadssynchronized(reportOutput){...}

If you can use the second for your purposes, give it a try and tell me if anything changes.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: Building DOM object from multiple threads

Thanks for the suggestion. I tried synchronizing on reportOutput - didnt resolve the issue.
On your question if they are writing to the same instance of XMLReportHandler - They are not. Below is the codet hats called from run()

(and the same pattern for all the other methods in the XMLReportHandler method which performs this synchronization)?

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: Building DOM object from multiple threads

Originally Posted by gdev

I am trying to build an XML format report with the results of the processing per record, while processing is underway per thread. The method below is called from every thread wvery time its ready to append an element to the DOM with the status.

After all priocessing completes, the below method is called only once by every thread to write to the File on the file system:

publicvoid writeToReportFile(){[...]}

Is the writeToReportFile() method called only once after processing from all the threads (that call writeReportLine() to build up reportOutput) completes? I'm asking because you wrote, "[writeToReportFile()] is called only once by every thread [...]." To put this in another way, is the method called once for each of the thread, or once for all the threads?

Originally Posted by gdev

The problem is that when under load, the threads just seem to hang while the transformer.transform(source, result) call keeps getting executed until there is an interrupt of some sort. I was able to examine a section of what was appended and it was status for records that had finished processing very early in the process based on my application logs.

By "transformer.transform(source, result) call keeps getting executed", do you mean that the transform() method is being called repeatedly, which would suggest that writeToReportFile() is being called repeatedly?

To further diagnose this, get a thread dump when the problem occurs. On Unix/Linux, a kill -quit <java_pid> would suffice. On Windows (also on Unix/Linux), use jstack. Before you do that, it'll help to name the threads (e.g., create your Threads using the Thread(String name) constructor) to make them more easily identifiable in the thread dump. Once you have the thread dump, you can either eyeball it, or use a tool such as https://java.net/projects/tda or https://www.ibm.com/developerworks/c...1-14c205f7333c (I usually use the latter) to help analyse it.