lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dawid Weiss <dawid.we...@gmail.com>
Subject Re: Immutable OpenBitSet?
Date Thu, 28 Apr 2011 08:27:08 GMT
> In general a *newly* created object that was not yet seen by any other
> thread is always safe. This is why I said, set all bits in the ctor. This
> is
> easy to understand: Before the ctor returns, the object's contents and all
> references like arrays are not seen by any other thread (that's
> guaranteed).
>

Yes, this is obvious, but simply having a static factory method like this:

public static MyComplexObject create() {
  return new MyComplexObject();
}

does not make the returned object consistent for multithreaded use simply
because nothing has been said about how this object is handed over to other
threads. If it's field-sharing (without volatiles and finals) then the
assigned reference (and whatever content it allocated) may be seen properly
on hardware architectures ensuring memory consistency, but can fail on other
architectures with relaxed caches requiring explicit memory barriers.

Like I said though -- incorrect programs work on most jvms anyway (either
because the hardware fixes the problem or the jvm is not aggressive enough
in the optimizations it could make).

Dawid

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message