[concurrency-interest] Primitives wrappers and visibility problems

Peter Veentjer alarmnummer at gmail.com
Wed Aug 23 07:05:57 EDT 2006


But what if it wasn't final. Example:

public class MyInt{
    private int value;

    public MyInt(int value){
        this.value = value;
    }

    public int getValue(){
        return value;
    }
}

Is the following class threadsafe?

1) because it is subject to out of order writes, other threads could
see a partially created MyInt (with the default value 0). A moment
later (when the constructor is called) they could see it with the
correct value.

2) because value is not safely published, it could happen that other
threads never see the new value.

So I would call this class not threadsafe.

Maybe something is wrong with my definition of threadsafe. Maybe I'm
missing something.

On 8/23/06, Ernst, Matthias <matthias.ernst at coremedia.com> wrote:
> > I have another question about visibility problems. Someone suggested
> > to me that the primitve wrappers (like Integer) don't use a final
> > field for the primitive they contain. This would make them not
> > threadsafe. Is this correct?
>
> I was about to write a lengthy response. It turns out that someone's
> suggestion is just wrong:
>
> java.lang.Integer at Java 1.5.0_07:
>    /**
>     * The value of the <code>Integer</code>.
>     *
>     * @serial
>     */
>    private final int value;
>
>
> Matthias
>


More information about the Concurrency-interest mailing list