[jdom-interest] Thread questions regarding JDOM SAXBuiler?

Laurent Bihanic laurent.bihanic at atosorigin.com
Mon Aug 30 03:22:54 PDT 2004


David Wall wrote:
> If I expect to do a lot of parsing with JDOM, it seems that I might want to
> create a pool of SAXBuilder objects to avoid the overhead of loading the
> parser.  Is that true, or is the overhead of creating one quite small?

The overhead of SAXBuilder in itself is not high, the problem comes from the 
underlying SAX parser. Creating a new Xerces instance for example requires a 
fair amount of CPU.

> Is a SAXBuilder thread-safe, or should I only be calling the
> SAXBuilder.build() method in a single thread at a time for a given
> SAXBuilder?

SAXBuilder is as thread-safe as the underlying SAX parser which means it is 
not thread-safe! SAX parser instances can be reused but not shared among threads.

> And I presume it's okay to reuse a SAXBuilder object by having many
> different threads build() documents over time without any issues that one
> XML parsing would affect the other (assuming they have the same options,
> like ignore whitespace, validation, etc.).

Correct. This has been validated with Xerces (2.4 and above) and Crimson (JDK 
1.3 and above).

> Is that the case?  If I do a lot of parsing (at least once per HTTP
> request), should I use a pool of SAXBuilder objects for this purpose, or is
> the overhead small enough that I can just create a new SAXBuilder whenever I
> want one?

If you are processing HTTP requests, I guess you are using a servlet 
container. In that case, the easiest solution is to make the SAXBuilder 
objects ThreadLocal variables. That way you piggyback the worker thread pool 
of the container rather that developing your own pool.

Code example was already posted to the list:
http://www.jdom.org/pipermail/jdom-interest/2004-July/013991.html

Laurent


More information about the jdom-interest mailing list