Java Reference
In-Depth Information
cutionException and rethrown from Future.get . This complicates code that calls
get , not only because it must deal with the possibility of ExecutionException (and
the unchecked CancellationException ), but also because the cause of the Execu-
tionException is returned as a Throwable , which is inconvenient to deal with.
When get throws an ExecutionException in Preloader , the cause will fall into
one of three categories: a checked exception thrown by the Callable , a RuntimeEx-
ception , or an Error . We must handle each of these cases separately, but we will use
the launderThrowable utility method in Listing 5.13 to encapsulate some of the messi-
er exception-handling logic. Before calling launderThrowable , Preloader tests for
the known checked exceptions and rethrows them. That leaves only unchecked exceptions,
which Preloader handles by calling launderThrowable and throwing the result. If
the Throwable passed to launderThrowable is an Error , launderThrowable
rethrows it directly; if it is not a RuntimeException , it throws an IllegalStateEx-
ception to indicate a logic error. That leaves only RuntimeException , which laun-
derThrowable returns to its caller, and which the caller generally rethrows.
Listing 5.13. Coercing an Unchecked Throwable to a RuntimeException .
5.5.3. Semaphores
Counting semaphores are used to control the number of activities that can access a certain
resource or perform a given action at the same time [CPJ 3.4.1]. Counting semaphores can be
used to implement resource pools or to impose a bound on a collection.
Search WWH ::




Custom Search