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() ...
Get Java Threads, Second 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.