[jdom-interest] Thread safe question: OK for simultaneous read-only access?
bob at werken.com
Sat Oct 27 23:49:46 PDT 2001
Reading and traversal are basically const operations (sorry for the
C++ throwback). They have no side-effects, and thus, there is no
way for one thread to alter another's view of the underlying data.
As soon as you throw even 1 mutating event into the mix, though,
the results quickly become undefined. Since the underlying storage
within the JDOM classes are from the Java Collections framework,
you'll see 'fail-fast' behaviour should you commit a synchronization
error. This occurs only within the context of a node, and not
an entire Document.
If Thread-A is reading Element-1's list of attributes, and Thread-B
alters Element-1's list of attributes simultaneously, you'll witness
an exception thrown to Thread-A. If Thread-B only modifies the
attributes of Element-5, then Thread-A will be unaware. While this
won't throw an exception to Thread-A, it might radically change
the semantics of the document unbeknownest to Thread-A.
If all threads *only* read, you're safe across the board.
If you want to read and write across a shared document, then I
suggest implementing a ReadWriteLock, associated with the Document
object, or similar, for maximum efficiency.
On Sat, 27 Oct 2001, Mark Bennett wrote:
> Sorry to be dense, but specifically, should simultaneous reading/traversal
> of a tree be OK? As long as no modifications are made?
> -----Original Message-----
> From: Jason Hunter [mailto:jhunter at acm.org]
> Sent: Saturday, October 27, 2001 10:18 AM
> To: mbennett at ideaeng.com
> Cc: jdom-interest at jdom.org
> Subject: Re: [jdom-interest] Thread safe question: OK for simultaneous
> read-only access?
> > JDOM is generally not thread safe, as I understand it.
> True. We follow the same model as ArrayList, which is not by default
> thread safe.
> > It's suggested that
> > it not be used in such an environment.
> Not true. It's simply suggested you manage your own thread safety. See
> the FAQ for an explanation.
> To control your jdom-interest membership:
More information about the jdom-interest