Re: stale objects in collections
Timo Nentwig wrote:
Eric Sosman wrote:
Timo Nentwig wrote On 08/21/06 15:15,:
In the WRONG example, isEmpty() is atomic and add() is atomic,
but the combination is not; the set is unlocked in between
That's clear...
You need to synchronize the different threads' accesses to
the set, or chaos will ensue. After you've joined all those
Why? So, the answer to my initial question whether a collection can
contain stale
data if multiple threads write to a non-synchronized collection is:
yes?
Have you considered doing things just a little differently?
Why not let each thread put its results in its own private set,
and then combine them when the threads are all finished? The
Well, sounds good despite I don't like copying data twice of not
neccessary. Anyway I want to *understand* why the collections must be
synchronized (as I thought).
You may be able to avoid the second copy, depending on how the result
set is used. For example, if it is only accessed through an iterator,
you could write your own iterator that goes through each per-thread set
in turn.
However, I would only do things like that after trying the simple
one-set approach, with synchronization, and finding a real bottleneck.
Patricia