[concurrency-interest] Pointless synchronized ?

Hanson Char hanson.char at gmail.com
Fri Apr 20 01:18:13 EDT 2007


Just removed some pointless synchronized from some pretty popular classes:

http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ToStringStyle.java?r1=500497&r2=530645
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java?r1=447989&r2=530648

Thanks,
Hanson Char

On 4/19/07, David Holmes <dcholmes at optusnet.com.au> wrote:
> Yes that synchronization is semantically pointless.
>
> David
>
> > -----Original Message-----
> > From: Hanson Char [mailto:hanson.char at gmail.com]
> > Sent: Friday, 20 April 2007 9:24 AM
> > To: dholmes at ieee.org
> > Cc: concurrency-interest
> > Subject: Re: [concurrency-interest] Pointless synchronized ?
> >
> >
> > 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