Java Reference
In-Depth Information
Listing 14.3. Bounded Buffer that Balks When Preconditions are Not Met.
Listing 14.4. Client Logic for Calling GrumpyBoundedBuffer .
A variant of this approach is to return an error value when the buffer is in the wrong state.
This is a minor improvement in that it doesn't abuse the exception mechanism by throwing an
exception that really means “sorry, try again”, but it does not address the fundamental prob-
lem: that callers must deal with precondition failures themselves. [2]
The client code in Listing 14.4 is not the only way to implement the retry logic. The caller
could retry the take immediately, without sleeping—an approach known as busy waiting
or spin waiting . This could consume quite a lot of CPU time if the buffer state does not
Search WWH ::




Custom Search