United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-2170881 : Remove AWT tree lock from Container methods: getComponent, getComponents, getComponentCount

Details
Type:
Backport
Submit Date:
2008-12-18
Status:
Resolved
Updated Date:
2011-01-19
Project Name:
JDK
Resolved Date:
2009-01-19
Component:
client-libs
OS:
Sub-Component:
java.awt
CPU:
Priority:
P2
Resolution:
Fixed
Affected Versions:
Fixed Versions:
6u12

Related Reports
Backport:

Sub Tasks

Description
                                    

Comments
EVALUATION

See parent CR for evaluation.
                                     
2008-12-18
SUGGESTED FIX

Changes to Container.java:

@@ -269,47 +269,52 @@
      * @deprecated As of JDK version 1.1,
      * replaced by getComponentCount().
      */
     @Deprecated
     public int countComponents() {
-        synchronized (getTreeLock()) {
+        // This method is not synchronized under AWT tree lock.
+        // Instead, the calling code is responsible for the
+        // synchronization. See 6784816 for details.
             return component.size();
         }
-    }
 
     /** 
      * Gets the nth component in this container.
      * @param      n   the index of the component to get.
      * @return     the n<sup>th</sup> component in this container.
      * @exception  ArrayIndexOutOfBoundsException  
      *                 if the n<sup>th</sup> value does not exist.     
      */
     public Component getComponent(int n) {
-        synchronized (getTreeLock()) {
-            if ((n < 0) || (n >= component.size())) {
-                throw new ArrayIndexOutOfBoundsException("No such child: " + n);
-            }
+        // This method is not synchronized under AWT tree lock.
+        // Instead, the calling code is responsible for the
+        // synchronization. See 6784816 for details.
+        try {
             return component.get(n);
+        } catch (IndexOutOfBoundsException z) {
+            throw new ArrayIndexOutOfBoundsException("No such child: " + n);
         }
     }
 
     /**
      * Gets all the components in this container.
      * @return    an array of all the components in this container.     
      */
     public Component[] getComponents() {
         return getComponents_NoClientCode();
     }
     // NOTE: This method may be called by privileged threads.
     //       This functionality is implemented in a package-private method 
     //       to insure that it cannot be overridden by client subclasses. 
     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
     final Component[] getComponents_NoClientCode() {
-        synchronized (getTreeLock()) {
+        // This method is not synchronized under AWT tree lock.
+        // Instead, the calling code is responsible for the
+        // synchronization. See 6784816 for details.
             return component.toArray(EMPTY_ARRAY);
         }
-    } // getComponents_NoClientCode()
 
     /**
      * Determines the insets of this container, which indicate the size 
      * of the container's border. 
      * <p>
                                     
2009-01-13



Hardware and Software, Engineered to Work Together