-
CSR
-
Resolution: Approved
-
P4
-
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:
- two mutators(add/remove) were marked as synchronized.
- 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;
- csr of
-
JDK-5031664 Increase thread safety of EventListenerList
-
- Resolved
-