[concurrency-interest] New bug pattern, way to common

Joshua Bloch josh at bloch.us
Fri Mar 14 16:32:57 EDT 2008


Bill,

Ouch!  "Synchronize only on final fields."

          Josh

P.S.  (No, I hadn't said that previously, but I will now.)



On Fri, Mar 14, 2008 at 1:08 PM, Bob Lee <crazybob at crazybob.org> wrote:

> Nice one.
>
>
> On Fri, Mar 14, 2008 at 12:28 PM, Bill Pugh <pugh at cs.umd.edu> wrote:
>
> > I identified a new concurrency bug pattern that is way too common:
> >
> > Synchronizing on a field in order to prevent concurrent access to that
> > field.
> >
> > For example:
> >   /**
> >      * Add a lifecycle event listener to this component.
> >      *
> >      * @param listener The listener to add
> >      */
> >     public void addInstanceListener(InstanceListener listener) {
> >
> >       synchronized (listeners) {
> >           InstanceListener results[] =
> >             new InstanceListener[listeners.length + 1];
> >           for (int i = 0; i < listeners.length; i++)
> >               results[i] = listeners[i];
> >           results[listeners.length] = listener;
> >           listeners = results;
> >       }
> >
> >     }
> >
> > There are 26 occurrences of this pattern in apache-tomcat.
> >
> > Of course, if the field in question is of type Boolean, it becomes
> > exceptionally problematical.
> >
> > Bill
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at altair.cs.oswego.edu
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080314/6ade018c/attachment.html 


More information about the Concurrency-interest mailing list