[concurrency-interest] Pointless synchronized ?

Hanson Char hanson.char at gmail.com
Thu Apr 19 19:24:10 EDT 2007


I see.  Good point.  In this case, however, the code is actually:

     private static ThreadLocal registry = new ThreadLocal() {
         protected synchronized Object initialValue() {
             return new HashMap();
         }
     };

Now, that's pretty pointless.  Right ?

Hanson Char

On 4/19/07, David Holmes <dcholmes at optusnet.com.au> wrote:
> Hanson,
>
> The value obtained from a ThreadLocal is a per-thread value, but the
> ThreadLocal object itself is shared by all threads that access it. So it is
> still possible to have state in a ThreadLocal object that is shared  mutable
> state and so needs protection from concurrent access. For a contrived
> example:
>
>   private static ThreadLocal counter = new ThreadLocal() {
>       private int initialCount = 0;
>
>       protected synchronized Object initialValue() {
>          return Integer.valueOf(initialCount++);
>       }
>   };
>
> Cheers,
> David Holmes
>
> > -----Original Message-----
> > From: concurrency-interest-bounces at cs.oswego.edu
> > [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Hanson
> > Char
> > Sent: Friday, 20 April 2007 6:50 AM
> > To: concurrency-interest
> > Subject: [concurrency-interest] Pointless synchronized ?
> >
> >
> > I see code (in some pretty common opensource projects) like:
> >
> >     private static ThreadLocal registry = new ThreadLocal() {
> >         protected synchronized Object initialValue() {
> >             // ...
> >         }
> >     };
> >
> > It seems pointless to have the initialValue method synchronized, since
> > it's thread safe by nature.
> >
> > Or am I missing something ?
> >
> > Hanson Char
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at altair.cs.oswego.edu
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>


More information about the Concurrency-interest mailing list