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

Bill Pugh pugh at cs.umd.edu
Fri Mar 14 15:28:30 EDT 2008


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


More information about the Concurrency-interest mailing list