6608764: PropertyChangeListeners machinery should have a better locking scheme
Summary: Change to use a private final object java.awt.Component.changeSupportLock for locking purposes instead of using this
Reviewed-by: son, ant
--- a/jdk/src/share/classes/java/awt/Component.java Fri Mar 14 22:00:33 2008 +0300
+++ b/jdk/src/share/classes/java/awt/Component.java Tue Mar 18 12:04:20 2008 +0300
@@ -634,6 +634,11 @@
*/
private PropertyChangeSupport changeSupport;
+ private transient final Object changeSupportLock = new Object();
+ private Object getChangeSupportLock() {
+ return changeSupportLock;
+ }
+
boolean isPacked = false;
/**
@@ -7839,15 +7844,17 @@
* @see #getPropertyChangeListeners
* @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
*/
- public synchronized void addPropertyChangeListener(
+ public void addPropertyChangeListener(
PropertyChangeListener listener) {
- if (listener == null) {
- return;
- }
- if (changeSupport == null) {
- changeSupport = new PropertyChangeSupport(this);
- }
- changeSupport.addPropertyChangeListener(listener);
+ synchronized (getChangeSupportLock()) {
+ if (listener == null) {
+ return;
+ }
+ if (changeSupport == null) {
+ changeSupport = new PropertyChangeSupport(this);
+ }
+ changeSupport.addPropertyChangeListener(listener);
+ }
}
/**
@@ -7863,12 +7870,14 @@
* @see #getPropertyChangeListeners
* @see #removePropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
*/
- public synchronized void removePropertyChangeListener(
+ public void removePropertyChangeListener(
PropertyChangeListener listener) {
- if (listener == null || changeSupport == null) {
- return;
- }
- changeSupport.removePropertyChangeListener(listener);
+ synchronized (getChangeSupportLock()) {
+ if (listener == null || changeSupport == null) {
+ return;
+ }
+ changeSupport.removePropertyChangeListener(listener);
+ }
}
/**
@@ -7885,11 +7894,13 @@
* @see java.beans.PropertyChangeSupport#getPropertyChangeListeners
* @since 1.4
*/
- public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
- if (changeSupport == null) {
- return new PropertyChangeListener[0];
- }
- return changeSupport.getPropertyChangeListeners();
+ public PropertyChangeListener[] getPropertyChangeListeners() {
+ synchronized (getChangeSupportLock()) {
+ if (changeSupport == null) {
+ return new PropertyChangeListener[0];
+ }
+ return changeSupport.getPropertyChangeListeners();
+ }
}
/**
@@ -7923,16 +7934,18 @@
* @see #getPropertyChangeListeners(java.lang.String)
* @see #addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
*/
- public synchronized void addPropertyChangeListener(
+ public void addPropertyChangeListener(
String propertyName,
PropertyChangeListener listener) {
- if (listener == null) {
- return;
- }
- if (changeSupport == null) {
- changeSupport = new PropertyChangeSupport(this);
- }
- changeSupport.addPropertyChangeListener(propertyName, listener);
+ synchronized (getChangeSupportLock()) {
+ if (listener == null) {
+ return;
+ }
+ if (changeSupport == null) {
+ changeSupport = new PropertyChangeSupport(this);
+ }
+ changeSupport.addPropertyChangeListener(propertyName, listener);
+ }
}
/**
@@ -7951,13 +7964,15 @@
* @see #getPropertyChangeListeners(java.lang.String)
* @see #removePropertyChangeListener(java.beans.PropertyChangeListener)
*/
- public synchronized void removePropertyChangeListener(
+ public void removePropertyChangeListener(
String propertyName,
PropertyChangeListener listener) {
- if (listener == null || changeSupport == null) {
- return;
- }
- changeSupport.removePropertyChangeListener(propertyName, listener);
+ synchronized (getChangeSupportLock()) {
+ if (listener == null || changeSupport == null) {
+ return;
+ }
+ changeSupport.removePropertyChangeListener(propertyName, listener);
+ }
}
/**
@@ -7974,12 +7989,14 @@
* @see #getPropertyChangeListeners
* @since 1.4
*/
- public synchronized PropertyChangeListener[] getPropertyChangeListeners(
+ public PropertyChangeListener[] getPropertyChangeListeners(
String propertyName) {
- if (changeSupport == null) {
- return new PropertyChangeListener[0];
- }
- return changeSupport.getPropertyChangeListeners(propertyName);
+ synchronized (getChangeSupportLock()) {
+ if (changeSupport == null) {
+ return new PropertyChangeListener[0];
+ }
+ return changeSupport.getPropertyChangeListeners(propertyName);
+ }
}
/**
@@ -7994,7 +8011,10 @@
*/
protected void firePropertyChange(String propertyName,
Object oldValue, Object newValue) {
- PropertyChangeSupport changeSupport = this.changeSupport;
+ PropertyChangeSupport changeSupport;
+ synchronized (getChangeSupportLock()) {
+ changeSupport = this.changeSupport;
+ }
if (changeSupport == null ||
(oldValue != null && newValue != null && oldValue.equals(newValue))) {
return;