[concurrency-interest] Primitives wrappers and visibility problems

Joe Bowbeer joe.bowbeer at gmail.com
Wed Aug 23 07:32:12 EDT 2006


The distinction between thread safety and safe publication was
discussed earlier on this list (Aug 2) as "concurrency errors in
java.lang.Throwable".

David Holmes wrote:

"Being safely publishable even when published without synchronization
goes a step beyond basic thread-safety."

Immutable classes like Integer are in fact easy to innoculate against
unsafe publication, but they are "thread-safe" (according to our
definition) even without the extra security boost.

Does the earlier discussion help clarify this?


On 8/23/06, Peter Veentjer <alarmnummer at gmail.com> wrote:
> 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