To synchronize part of a method preceded by synchronized(this). : Synchronize « Thread « SCJP






import java.awt.Rectangle;

class MyClass {
  Rectangle rect = new Rectangle(1, 1, 100, 300);

  void doit() {
    int x = 504;
    int y = x / 3;
    synchronized (this) {
      rect.width -= x;
      rect.height -= y;
    }
  }
}








7.9.Synchronize
7.9.1.synchronized code
7.9.2.Synchronize an entire method by putting the synchronized modifier in the method's declaration.
7.9.3.Synchronizing Part of a Method
7.9.4.An example for deadlock
7.9.5.To synchronize part of a method preceded by synchronized(this).
7.9.6.When used with a code block, synchronized must be applied to an object.