O'Reilly logo

Graphic Java™ 1.2, Volume I: AWT, Third Edition by David M. Geary

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required