Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8191167

Increase thread safety of EventListenerList

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 10
    • client-libs
    • None
    • behavioral
    • minimal
    • Java API
    • SE

      Summary

      Improve thread safely of javax.swing.event.EventListenerList by marking the listenerList field as volatile.

      Problem

      The EventListenerList class was implemented to be a thread safe. To achieve the correct state of the object:

      1. two mutators(add/remove) were marked as synchronized.
      2. the internal array is updated via copy-on-write, the internal array is never modified after it was set.

      It was assumed that the getXXX methods will get the value which was already set by the mutator or the previous value if mutator is in-progress but was not update the array. The problem is that the getXXX is not necessary read the value which was set, because of lack of happens-before between add/remove and getXXX.

      Solution

      The array of listeners was marked as volatile to solve the problem.

      Specification

      diff -r d4ed3b8d166c src/java.desktop/share/classes/javax/swing/event/EventListenerList.java
      --- a/src/java.desktop/share/classes/javax/swing/event/EventListenerList.java   Mon Nov 13 15:04:18 2017 -0800  
      +++ b/src/java.desktop/share/classes/javax/swing/event/EventListenerList.java   Mon Nov 13 16:35:55 2017 -0800
      @@ -102,7 +102,7 @@
           /** The list of ListenerType - Listener pairs */
      -    protected transient Object[] listenerList = NULL_ARRAY;
      + protected transient volatile Object[] listenerList = NULL_ARRAY;

            serb Sergey Bylokhov
            jleesunw Jon Lee (Inactive)
            Alexander Zvegintsev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: