[jdom-interest] Is saxbuilder safe for multi-threaded (concurrent) use?

Rolf Lear jdom at tuis.net
Tue Nov 8 08:45:10 PST 2011

To clarify this all, it all boils down to the fact that XMLReader (and
other SAX classes/interfaces) are not specified to be Thread-Safe, so, at
it's lowest level, SAXBuilder can never guarantee to be thread-safe.
SAXBuilder has other internal fields that are not protected either, but it
would be pointless to protect them since the actual parser itself is not

The safest thing to do (the only safe thing to do) is to ensure that you
never use a SAXBuilder across threads.

There are lots of 'patterns' you can use to ensure this. Ones that come to
mind are:
- create a new SAXBuilder instance each time you need one
- create a 'pool' and check-out/check-in each builder.
- 'synchronize' the block containing the SAXBuilder.
- using thread-locals
- ....

The pattern you use is very dependent on your particular circumstances...
I can't recommend any particular one.

In the most common case, though, where most time is spent actually
processing the document information, rather than parsing the input, is to
just create a parser when you need it...

In tight-loops, like Cliff is doing, I would just initialiase each thread
with it's own SAXBuilder, and reuse it in the thread.

More complicated patterns that that are probably more 'expensive' to
manage than any 'savings' that can be found.


On Tue, 8 Nov 2011 17:02:08 +0100, Paul Libbrecht <paul at hoplahup.net>
> That means the best design pattern is to use threadlocals with these or?
> paul
> Le 8 nov. 2011 à 16:44, Rolf Lear a écrit :
>> SAXBuilder is *not* thread-safe, but it is reusable....
>> i.e. you need one SAXBuilder per thread, but after parsing something,
>> can reuse the builder for the next parse.
>> You can run many different SAXBuilders 'in parallel', just so long as
>> specific SAXBuilder instance is referenced from different threads.
>> For improved performance set the fast reconfigure and reuseParser flags
>> on
>> the builder:
>> builder.setReuseParser(true);
>> builder.setFastReconfigure(true);
>> Rolf
>> On Tue, 8 Nov 2011 10:23:06 -0500, cliff palmer <palmercliff at gmail.com>
>> wrote:
>>> I have a large number (hundreds of thousands) of XML streams embedded
>>> files to examine and was considering a multi-threaded design for the
>> java
>>> code, dispatching the streams to a number of worker threads that would
>> call
>>> Saxbuilder.build() then continue with the needed work.  I'm wondering
>>> Saxbuilder.build, and the other JDOM code on which it relies, is
>>> safe" so that I don't run into problems with concurrency using it.
>>> Obviously my code will have to (also) be concurrency-friendly as well
>> and I
>>> will have to manage resources carefully.
>>> Thanks in advance.
>>> Cliff
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com

More information about the jdom-interest mailing list