[concurrency-interest] Explicitly initializing volatile fields with default values

thurstonn thurston at nomagicsoftware.com
Thu Dec 17 20:30:07 EST 2015


What about


class B
{
    int f = 42;
    boolean initialized = true;
    volatile int v = 0;


}

B global;

Thread 1:
   global = new B();

 Thread 2:
   B b = global;
   if (b != null && b.initialized && b.v == 0) {
     print(b.f); // ?
   } 

I still think that the JMM allows either 0 or 42.

"There is a gut feeling that removing these initializations is *safe*"

But partly it comes down to your definition of "safe", i.e.

1.  Safe ==> spec-compliant
or
2.  Safe ==> no JVM presently in use will behave differently in the face of
initialization-to-default-value elision






--
View this message in context: http://jsr166-concurrency.10961.n7.nabble.com/Explicitly-initializing-volatile-fields-with-default-values-tp13093p13097.html
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.


More information about the Concurrency-interest mailing list