Java Reference
In-Depth Information
The JDBC driver being used here clearly has a lock-ordering bug: different call chains
through the JDBC driver acquire multiple locks in different orders. But this problem would
not have manifested itself were it not for another bug: multiple threads were trying to use the
same JDBC Connection at the same time. This was not how the application was supposed
to work—the developers were surprised to see the same Connection used concurrently by
two threads. There's nothing in the JDBC specification that requires a Connection to be
thread-safe, and it is common to confine use of a Connection to a single thread, as was
intended here. This vendor tried to deliver a thread-safe JDBC driver, as evidenced by the
synchronization on multiple JDBC objects within the driver code. Unfortunately, because the
vendor did not take lock ordering into account, the driver was prone to deadlock, but it was
only the interaction of the deadlock-prone driver and the incorrect Connection sharing by
the application that disclosed the problem. Because neither bug was fatal in isolation, both
persisted despite extensive testing.
10.3. Other Liveness Hazards
While deadlock is the most widely encountered liveness hazard, there are several other live-
ness hazards you may encounter in concurrent programs including starvation, missed signals,
and livelock. (Missed signals are covered in Section 14.2.3 . )
10.3.1. Starvation
Starvation occurs when a thread is perpetually denied access to resources it needs in order
to make progress; the most commonly starved resource is CPU cycles. Starvation in Java ap-
plications can be caused by inappropriate use of thread priorities. It can also be caused by
executing nonterminating constructs (infinite loops or resource waits that do not terminate)
with a lock held, since other threads that need that lock will never be able to acquire it.
The thread priorities defined in the Thread API are merely scheduling hints. The Thread API
defines ten priority levels that the JVM can map to operating system scheduling priorities as
it sees fit. This mapping is platform-specific, so two Java priorities can map to the same OS
priority on one system and different OS priorities on another. Some operating systems have
fewer than ten priority levels, in which case multiple Java priorities map to the same OS pri-
ority.
Operating system schedulers go to great lengths to provide scheduling fairness and liveness
beyond that required by the Java Language Specification. In most Java applications, all ap-
plication threads have the same priority, Thread. NORM_PRIORITY . The thread priority
mechanism is a blunt instrument, and it's not always obvious what effect changing priorities
Search WWH ::




Custom Search