Java Reference
In-Depth Information
human brain is thought to contain billions of “processors.” Today's multi-core computers
have multiple processors that can perform tasks in parallel.
Java Concurrency
Java makes concurrency available to you through the language and APIs. Java programs
can have multiple threads of execution , where each thread has its own method-call stack
and program counter, allowing it to execute concurrently with other threads while sharing
with them application-wide resources such as memory and file handles. This capability is
called multithreading .
Performance Tip 23.1
A problem with single-threaded applications that can lead to poor responsiveness is that
lengthy activities must complete before others can begin. In a multithreaded application,
threads can be distributed across multiple cores (if available) so that multiple tasks execute
in parallel and the application can operate more efficiently. Multithreading can also increase
performance on single-processor systems—when one thread cannot proceed (because, for ex-
ample, it's waiting for the result of an I/O operation), another can use the processor.
Concurrent Programming Uses
We'll discuss many applications of concurrent programming . For example, when stream-
ing an audio or video over the Internet, the user may not want to wait until the entire au-
dio or video downloads before starting the playback. To solve this problem, multiple
threads can be used—one to download the audio or video (later in the chapter we'll refer
to this as a producer ), and another to play it (later in the chapter we'll refer to this as a con-
sumer ). These activities proceed concurrently. To avoid choppy playback, the threads are
synchronized (that is, their actions are coordinated) so that the player thread doesn't begin
until there's a sufficient amount of the audio or video in memory to keep the player thread
busy. Producer and consumer threads share memory —we'll show how to coordinate these
threads to ensure correct execution. The Java Virtual Machine (JVM) creates threads to
run programs and threads to perform housekeeping tasks such as garbage collection.
Concurrent Programming Is Difficult
Writing multithreaded programs can be tricky. Although the human mind can perform
functions concurrently, people find it difficult to jump between parallel trains of thought.
To see why multithreaded programs can be difficult to write and understand, try the fol-
lowing experiment: Open three books to page 1, and try reading the topics concurrently.
Read a few words from the first book, then a few from the second, then a few from the
third, then loop back and read the next few words from the first book, and so on. After
this experiment, you'll appreciate many of the challenges of multithreading—switching
between the topics, reading briefly, remembering your place in each book, moving the
book you're reading closer so that you can see it and pushing the topics you're not reading
aside—and, amid all this chaos, trying to comprehend the content of the topics!
Use the Prebuilt Classes of the Concurrency APIs Whenever Possible
Programming concurrent applications is difficult and error prone. If you must use synchro-
nization in a program, follow these guidelines:
1. The vast majority of programmers should use existing collection classes and interfaces
from the concurrency APIs that manage synchronization for you—such as the Array-
 
Search WWH ::




Custom Search