Java Reference
In-Depth Information
Cancellation can involve state other than the interruption status; interruption can be used to
get the thread's attention, and information stored elsewhere by the interrupting thread can be
used to provide further instructions for the interrupted thread. (Be sure to use synchronization
when accessing that information.)
Listing 7.7. Noncancelable Task that Restores Interruption Before Exit.
For example, when a worker thread owned by a ThreadPoolExecutor detects interrup-
tion, it checks whether the pool is being shut down. If so, it performs some pool cleanup be-
fore terminating; otherwise it may create a new thread to restore the thread pool to the desired
size.
7.1.4. Example: Timed Run
Many problems can take forever to solve (e.g., enumerate all the prime numbers); for others,
the answer might be found reasonably quickly but also might take forever. Being able to say
“spend up to ten minutes looking for the answer” or “enumerate all the answers you can in
ten minutes” can be useful in these situations.
The aSecondOfPrimes method in Listing 7.2 starts a PrimeGenerator and interrupts
it after a second. While the PrimeGenerator might take somewhat longer than a second
Search WWH ::




Custom Search