On Thu, Apr 28, 2011 at 6:13 PM, Uwe Schindler <u...@thetaphi.de> wrote:
> 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).

Section 17.5 of the JLS gives the following example:

    class FinalFieldExample {
      final int x;
      int y;
      static FinalFieldExample f;
      public FinalFieldExample() {
        x = 3;
        y = 4;
      }
      static void writer() {
        f = new FinalFieldExample();
      }
      static void reader() {
        if (f != null) {
          int i = f.x; // guaranteed to see 3
          int j = f.y; // could see 0
        }
      }
    }

Essentially, there is no guarantee that the work in the constructor
has been completed when another thread gets a reference to the object.
 To make the guarantee, use the final keyword.

It seems like this contradicts the claim above, but maybe I'm missing something.

TX

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org
For additional commands, e-mail: java-user-h...@lucene.apache.org

Reply via email to