Components and Tree Locking

The Component class instantiates a static final Object, referred to as the tree lock, that is used to synchronize access to a component's container hierarchy and layout methods. The idea is to prevent two threads from simultaneously accessing a component's hierarchy of containers (and layout methods), which could result in an inconsistent state without synchronizing access. For instance, the Container class uses the tree lock when removing a component, as listed in Example 11-12.

Example 11-12. java.awt.Container.remove() Uses the Component Tree Lock
// from java.awt.Container class ...

public void remove(int index) {
   synchronized (Component.LOCK) {
      ...
   }
}

Container.remove() synchronizes on the Component tree lock. ...

Get Graphic Java™ 1.2, Volume I: AWT, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.