Java Reference
In-Depth Information
Section 23.14 (Advanced) Interfaces Callable and Future
•The Callable (p. 1030) interface (of package java.util.concurrent ) declares a single method
named call that allows a task to return a value.
ExecutorService method submit (p. 1030) executes a Callable passed in as its argument. Meth-
od submit returns an object of type Future (of package java.util.concurrent ) that represents
the future result of the executing Callable .
• Interface Future (p. 1030) declares method get to return the result of the Callable . The inter-
face also provides methods that enable you to cancel a Callable 's execution, determine whether
the Callable was cancelled and determine whether the Callable completed its task.
• Java SE 8 introduces a new CompletableFuture class (package java.util.concurrent ; p. 1030),
which implements the Future interface and enables you to asynchronously execute Runnable s
that perform tasks or Supplier s that return values.
• Interface Supplier (p. 1030), like interface Callable , is a functional interface with a single meth-
od (in this case, get ) that receives no arguments and returns a result.
CompletableFuture static method supplyAsync (p. 1033) asynchronously executes a Supplier
task that returns a value.
CompletableFuture static method runAsync (p. 1034) asynchronously executes a Runnable task
that does not return a result.
CompletableFuture method get is a blocking method—it causes the calling thread to wait until
the asynchronous task completes and returns its results.
Section 23.15 (Advanced) Fork/Join Framework
• Java's concurrency APIs include the fork/join framework, which helps programmers parallelize
algorithms. The fork/join framework particularly well suited to divide-and-conquer-style algo-
rithms, like the merge sort.
Self-Review Exercises
23.1
Fill in the blanks in each of the following statements:
a) A thread enters the terminated state when .
b) To pause for a designated number of milliseconds and resume execution, a thread
should call method of class .
c) A runnable thread can enter the state for a specified interval of time.
d) At the operating-system level, the runnable state actually encompasses two separate
states, and .
e) Runnable s are executed using a class that implements the interface.
f) ExecutorService method ends each thread in an ExecutorService as soon as
it finishes executing its current Runnable , if any.
g)
In a(n)
relationship, the
generates data and stores it in a shared ob-
ject, and the
reads data from the shared object.
h)
Keyword
indicates that only one thread at a time should execute on an object.
23.2
(Advanced Optional Sections) Fill in the blanks in each of the following statements:
a)
Method of class Condition moves a single thread in an object's waiting state
to the runnable state.
b)
Method of class Condition moves every thread in an object's waiting state to
the runnable state.
c)
A thread can call method
on a Condition object to release the associated Lock
and place that thread in the
state.
 
Search WWH ::




Custom Search