Java Reference
In-Depth Information
Interruption is usually the most sensible way to implement cancellation.
BrokenPrimeProducer can be easily fixed (and simplified) by using interruption in-
stead of a boolean flag to request cancellation, as shown in Listing 7.5 . There are two points
in each loop iteration where interruption may be detected: in the blocking put call, and by
explicitly polling the interrupted status in the loop header. The explicit test is not strictly
necessary here because of the blocking put call, but it makes PrimeProducer more re-
sponsive to interruption because it checks for interruption before starting the lengthy task of
searching for a prime, rather than after. When calls to interruptible blocking methods are not
frequent enough to deliver the desired responsiveness, explicitly testing the interrupted status
can help.
Listing 7.5. Using Interruption for Cancellation.
Search WWH ::




Custom Search