O'Reilly logo

Java Threads, Second Edition by Henry Wong, Scott Oaks

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

The Synchronized Block

Notice that the original getBusyFlag() method is not declared as synchronized. This is because it’s not necessary: getBusyFlag() does not try to access the busyflag variable. Instead, it calls the tryGetBusyFlag() method, which accesses the busyflag and is, of course, synchronized. Let’s take another look at the getBusyFlag() method, one that does not call the tryGetBusyFlag() method. Instead, this version gets the busyflag directly:

public synchronized void getBusyFlag() {
     while (true) {
          if (busyflag == null) {
               busyflag = Thread.currentThread();
               break;
          }
          try {
               Thread.sleep(100);
          } catch (Exception e) {}
     }
}

Let’s assume that we do not want the inefficiency of an extra method call to the tryGetBusyFlag() method. In our new version of the getBusyFlag() method, we now access the busyflag directly. The getBusyFlag() method simply loops waiting for the flag to be freed, sets the flag, and returns. Since we are now accessing the busyflag directly, we must make the method synchronized or we will have a race condition.

Unfortunately, there is a problem when we declare this method to be synchronized. While declaring the method synchronized prevents other getBusyFlag() and tryGetBusyFlag() methods from being run at the same time (which prevents a race condition), it also prevents the freeBusyFlag() method from running. This means that if the flag is busy when getBusyFlag() is called, getBusyFlag() waits until the flag is freed. Unfortunately, since the freeBusyFlag() ...

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