[concurrency-interest] BigDecimal Safe Publication

James james at inaseq.com
Mon Aug 20 06:58:41 EDT 2012


I'm wondering whether the BigDecimal constructors that take a MathContext parameter exhibit initialization safety.  As far as I can tell (and I could be very wrong), BigDecimal is relying on the volatile nature of the intVal reference to ensure the BigDecimal is effectively immutable.  However, the constructors that take a MathContext delegate to the following method, which alters other members after intVal:

    private void roundThis(MathContext mc) {
        BigDecimal rounded = doRound(this, mc);
        if (rounded == this)                 // wasn't rounded
            return;
        this.intVal     = rounded.intVal;
        this.intCompact = rounded.intCompact;
        this.scale      = rounded.scale;
        this.precision  = rounded.precision;
    }

Is this a potential issue for safe publication or am I missing something?
Is BigDecimal intended to exhibit initialization safety?


More information about the Concurrency-interest mailing list