Java Reference
In-Depth Information
Collections.synchronizedList() , you'll still need to synchronize on it if you want to
iterate through the list because that involves many consecutive atomic operations. Al‐
though each method call is safely atomic, the sequence of operations is not without
explicit synchronization.
Deadlock
Synchronization can lead to another possible problem: deadlock . Deadlock occurs when
two threads need exclusive access to the same set of resources and each thread holds
the lock on a different subset of those resources. If neither thread is willing to give up
the resources it has, both threads come to an indefinite halt. This isn't quite a hang in
the classical sense because the program is still active and behaving normally from the
perspective of the OS, but to a user the difference is insignificant.
To return to the library example, deadlock is what occurs when Jack and Jill are each
writing a term paper on Thomas Jefferson and they both need the two topics Thomas
Jefferson and Sally Hemings: An American Controversy and Sally Hemings and Thomas
Jefferson: History, Memory, and Civic Culture . If Jill has checked out the first topic and
Jack has checked out the second, and neither is willing to give up the topic they have,
neither can finish the paper. Eventually, the deadline expires and they both get an F.
That's the problem of deadlock.
Worse yet, deadlock can be a sporadic, hard-to-detect bug. Deadlock usually depends
on unpredictable issues of timing. Most of the time, either Jack or Jill will get to the
library first and check out both topics. In this case, the one who gets the topic first
writes a paper and returns the topics; then the other one gets the topics and writes his
or her paper. Only rarely will they arrive at the same time and each get one of the two
topics. Ninety-nine times out of 100 or 999 times out of 1,000, a program will run to
completion perfectly normally. Only rarely will it hang for no apparent reason. Of
course, if a multithreaded server is handling hundreds of requests per second, even a
problem that occurs only once every million requests can hang the server in short order.
The most important technique for preventing deadlock is to avoid unnecessary syn‐
chronization. If there's an alternative approach for ensuring thread safety, such as mak‐
ing objects immutable or keeping a local copy of an object, use it. Synchronization
should be a last resort for ensuring thread safety. If you do need to synchronize, keep
the synchronized blocks small and try not to synchronize on more than one object at a
time. This can be tricky, though, because many of the methods from the Java class library
that your code may invoke synchronize on objects you aren't aware of. Consequently,
you may in fact be synchronizing on many more objects than you expect.
The best you can do in the general case is carefully consider whether deadlock is likely
to be a problem and design your code around it. If multiple objects need the same set
of shared resources to operate, make sure they request them in the same order. For
Search WWH ::




Custom Search