23Concurrency
Objectives
In this chapter you’ll:
Understand concurrency,
parallelism and
Use JavaFX’s concurrency APIs
to update GUIs in a thread-
safe manner.
Compare the performance of
Arrays methods sort and
2Chapter 23 Concurrency
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
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) Runnables are executed using a class that implements the interface.
f) ExecutorService method prevents the ExecutorService from accepting new
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) Only one thread at a time can execute a(n) statement or block.
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.
d) Class implements the BlockingQueue interface using an array.
e) Class Instant’s static method gets the current time.
f) Duration method returns the Duration as a long value milliseconds.
g) NumberFormat static method returns a NumberFormat that’s used to format
a number as a percentage.
h) NumberFormat method returns a String representation of its argument in the
specified numeric format.
i) Arrays static method fills an array with values produced by a generator
function.
j) Arrays static method applies a BinaryOperator to the current and previous
array elements and stores the result in the current element.
Exercises 3
k) To obtain a parallel stream, simply invoke method on an existing stream.
l) Among its many features a CompletableFuture enables you to asynchronously execute
that perform tasks or that return values.
23.3 State whether each of the following is true or false. If false, explain why.
a) A thread is not runnable if it has terminated.
b) Some operating systems use timeslicing with threads. Therefore, they can enable threads
to preempt threads of the same priority.
c) When the thread’s quantum expires, the thread returns to the running state as the op-
erating system assigns it to a processor.
d) On a single-processor system without timeslicing, each thread in a set of equal-priority
threads (with no other threads present) runs to completion before other threads of equal
priority get a chance to execute.
23.4 (Advanced Optional Sections) State whether each of the following is true or false. If false,
explain why.
a) To determine the difference between two Instants, use class Duration’s static method
difference, which returns a Duration object containing the time difference.
b) Streams are easy to parallelize, enabling programs to benefit from enhanced perfor-
c) Interface Supplier, like interface Callable, is a functional interface with a single meth-
od that receives no arguments and returns a result.
d) CompletableFuture static method runAsync asynchronously executes a Supplier task
that returns a value.
e) CompletableFuture static method supplyAsync asynchronously executes a Runnable
task that does not return a result.
Exercises
NOTE: Solutions to the programming exercises are located in the ch23solutions folder.
Each exercise has its own folder named ex23_## where ## is a two-digit number represent-
ing the exercise number. For example, exercise 23.10’s solution is located in the folder
ex20_10.
23.5 (True or False) State whether each of the following is true or false. If false, explain why.
a) Method sleep does not consume processor time while a thread sleeps.
4Chapter 23 Concurrency
b) JavaFX components are thread safe.
c) (Advanced) Declaring a method synchronized guarantees that deadlock cannot occur.
d) (Advanced) Once a ReentrantLock has been obtained by a thread, the ReentrantLock
object will not allow another thread to obtain the lock until the first thread releases it.
23.6 (Multithreading Terms) Define each of the following terms.
a) thread
b) multithreading
c) runnable state
d) timed waiting state
e) preemptive scheduling
f) Runnable interface
g) producer/consumer relationship
h) quantum
23.7 (Advanced: Multithreading Terms) Discuss each of the following terms in the context of
Java’s threading mechanisms:
a) synchronized
b) wait
c) notify
d) notifyAll
e) Lock
Exercises 5
f) Condition
23.8 (Blocked State) List the reasons for entering the blocked state. For each of these, describe
how the program will normally leave the blocked state and enter the runnable state.
23.9 (Deadlock and Indefinite Postponement) Two problems that can occur in systems that al-
low threads to wait are deadlock, in which one or more threads will wait forever for an event that
cannot occur, and indefinite postponement, in which one or more threads will be delayed for some
unpredictably long time. Give an example of how each of these problems can occur in multithread-
ed Java programs.