Java Reference
In-Depth Information
23.1 Introduction
23.2 Thread States and Life Cycle
23.2.1 New and Runnable States
23.2.2 Waiting State
23.2.3 Timed Waiting State
23.2.4 Blocked State
23.2.5 Ter mina ted State
23.2.6 Operating-System View of the
Runnable State
23.2.7 Thread Priorities and Thread
Scheduling
23.2.8 Indefinite Postponement and
Deadlock
23.3 Creating and Executing Threads with
the Executor Framework
23.4 Thread Synchronization
23.4.1 Immutable Data
23.4.2 Monitors
23.4.3 Unsynchronized Mutable Data
Sharing
23.4.4 Synchronized Mutable Data
Sharing—Making Operations Atomic
23.5 Producer/Consumer Relationship
without Synchronization
23.6 Producer/Consumer Relationship:
ArrayBlockingQueue
23.7 (Advanced) Producer/Consumer
Relationship with synchronized ,
wait , notify and notifyAll
23.8 (Advanced) Producer/Consumer
Relationship: Bounded Buffers
23.9 (Advanced) Producer/Consumer
Relationship: The Lock and
Condition Interfaces
23.10 Concurrent Collections
23.11 Multithreading with GUI:
SwingWorker
23.11.1 Performing Computations in a
Worker Thread: Fibonacci Numbers
23.11.2 Processing Intermediate Results: Sieve
of Eratosthenes
23.12 sort / parallelSort Timings with
the Java SE 8 Date/Time API
23.13 Java SE 8: Sequential vs. Parallel
Streams
23.14 (Advanced) Interfaces Callable
and Future
23.15 (Advanced) Fork/Join Framework
23.16 Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises
23.1 Introduction
[Note: Sections marked “Advanced” are intended for readers who wish a deeper treatment of con-
currency and may be skipped by readers preferring only basic coverage.] It would be nice if we
could focus our attention on performing only one task at a time and doing it well. That's
usually difficult to do in a complex world in which there's so much going on at once. This
chapter presents Java's capabilities for developing programs that create and manage multiple
tasks. As we'll demonstrate, this can greatly improve program performance.
When we say that two tasks are operating concurrently , we mean that they're both
making progress at once. Until recently, most computers had only a single processor. Oper-
ating systems on such computers execute tasks concurrently by rapidly switching between
them, doing a small portion of each before moving on to the next, so that all tasks keep
progressing. For example, it's common for personal computers to compile a program, send
a file to a printer, receive electronic mail messages over a network and more, concurrently.
Since its inception, Java has supported concurrency.
When we say that two tasks are operating in parallel , we mean that they're executing
simultaneously . In this sense, parallelism is a subset of concurrency. The human body per-
forms a great variety of operations in parallel. Respiration, blood circulation, digestion,
thinking and walking, for example, can occur in parallel, as can all the senses—sight,
hearing, touch, smell and taste. It's believed that this parallelism is possible because the
 
 
Search WWH ::




Custom Search