Synchronizing on Objects

Magic Feather example.

Each object has a magic feather called a monitor. When you use the synchronized keyword on a method, you're gaining the monitor of the current object or locking it. As long as a thread possesses the monitor or lock of an object no other thread can get the lock for that object. (Another thread may be able to lock a different object in the same class, however.)

When you use the synchronized modifier to specify that a method is synchronized, you are locking on the particular object whose method is invoked. (Static methods can also be synchronized. In this case, the lock is placed on the java.lang.Class object associated with the object's class.) Since there's only one lock per object, it's not just the synchronized method that can't be invoked by other threads. It's any other synchronized method or block of code in that object.

You can synchronize at a lower level than a method. For example the following program had problems if another thread changed the value of i or this.i while the assignment was taking place.

public class Counter {

  int i = 0;

  public void count() {
    int i = this.i;
    int limit = i + 100;
    while (i++ != limit)  System.out.println(i);
    this.i = i; 
  }

}
You can fix this by synchronizing the lines of code that refer to the field:

  public void count() {
    synchronized (this) {
      int i = this.i;
    }
    int limit = i + 100;
    while (i++ != limit) System.out.println(i);
    synchronized (this) {
      this.i = i; 
    }
  }

Previous | Next | Top
Last Modified December 2, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu