Java Reference
In-Depth Information
We discussed the life cycle of a thread and the states that a thread may occupy during
its lifetime. Next, we presented the interface Runnable , which is used to specify a task that
can execute concurrently with other tasks. This interface's run method is invoked by the
thread executing the task. We showed how to execute a Runnable object by associating it
with an object of class Thread . Then we showed how to use the Executor interface to
manage the execution of Runnable objects via thread pools, which can reuse existing
threads to eliminate the overhead of creating a new thread for each task and can improve
performance by optimizing the number of threads to ensure that the processor stays busy.
You learned that when multiple threads share an object and one or more of them
modify that object, indeterminate results may occur unless access to the shared object is
managed properly. We showed you how to solve this problem via thread synchronization,
which coordinates access to shared mutable data by multiple concurrent threads. You
learned several techniques for performing synchronization—first with the built-in class
ArrayBlockingQueue (which handles all the synchronization details for you), then with
Java's built-in monitors and the synchronized keyword, and finally with interfaces Lock
and Condition .
We discussed the fact that Swing GUIs are not thread safe, so all interactions with and
modifications to the GUI must be performed in the event dispatch thread. We also dis-
cussed the problems associated with performing long-running calculations in the event
dispatch thread. Then we showed how you can use the SwingWorker class to perform long-
running calculations in worker threads. You learned how to display the results of a Swing-
Worker in a GUI when the calculation completed and how to display intermediate results
while the calculation was still in process.
We revisited the Arrays class's sort and parallelSort methods to demonstrate the
benefit of using a parallel sorting algorithm on a multi-core processor. We used the Java
SE 8 Date/Time API's Instant and Duration classes to time the sort operations.
You learned that Java SE 8 streams are easy to parallelize, enabling programs to benefit
from enhanced performance on multi-core systems, and that to obtain a parallel stream,
you simply invoke method parallel on an existing stream.
We discussed the Callable and Future interfaces, which enable you to execute tasks
that return results and to obtain those results, respectively. We then presented an example
of performing long-running tasks synchronously and asynchronously using Java SE 8's
new CompletableFuture class. We use the multithreading techniques introduced in this
chapter again in Chapter 28, Networking, to help build multithreaded servers that can
interact with multiple clients concurrently. In the next chapter, we introduce database-
application development with Java's JDBC API.
Summary
Section 23.1 Introduction
• Two tasks that are operating concurrently are both making progress at once.
• Two tasks that are operating in parallel are executing simultaneously. In this sense, parallelism is
a subset of concurrency. Today's multi-core computers have multiple processors that can per-
form tasks in parallel.
• Java makes concurrency available to you through the language and APIs.
Search WWH ::




Custom Search