Java Reference
In-Depth Information
Multithreading
Multithreading is the coordination of the various threads that run in a system. It is done for
the purpose of improving overall system efficiency. This could mean taking turns with the
resources or sharing them, decreasing overhead, or respecting the boundaries of other
threads.
There are inherent challenges in managing multiple threads. These challenges arise as a
result of the way in which central processing units (CPUs) handle threads. We will be discussing
these challenges throughout this chapter, with explicit details in the section “Understanding
Thread Safety.”
A CPU will execute a number of instructions from a given thread, then switch over to exe-
cuting a set of instructions from another thread, and so on. Because this switching happens
quickly, an illusion of independent and parallel execution is created for all the threads. In
addition, threads that are inactively waiting—say, for a network connection—will not act as
bottlenecks for other threads. This is one of the greatest advantages of multithreading. How-
ever, this increased efficiency comes at a price: It is entirely possible for one thread to corrupt
the data that another thread is using, unless precautions are taken. We will demonstrate these
problems, and show you how to program defensively to counteract these problems in the sec-
tion “Understanding Thread Safety.”
Note Throughout this chapter we will be discussing threading from the perspective of the JVM operating
on a single CPU computer. On computers with multiple CPUs, it is quite possible for threads to be operating
on different CPUs concurrently. Regardless, the issues that arise will be the same.
Returning to the word processing program example, suppose that another thread is justi-
fying the text, and this thread makes no effort to coordinate with the thread that is printing
the document. The result might be that some of the printed document is justified and some
of it is not.
These are some of the challenges that come with the territory when you start to multi-
thread. The SCJD exam requires that you have a strong understanding of multithreading and
the dangers that lurk therein.
Java's Multithreading Concepts
Java is one of the few languages that has built-in support for multithreading. Two important
behaviors arise from this support. First, every Java object can be locked down for exclusive use
by a given thread. Synchronizing on an object achieves this. A locked object is inaccessible to
any thread other than the one that explicitly claimed it as long as all the other threads honor
the locking. Second, each Java object can keep track of all the threads that want exclusive
access to it. Think of this as having a sign-up sheet for each object.
The basic challenge of multithreading is similar to one of the challenges of raising young
children: How do you keep the threads (children) from fighting over a limited supply of
resources (toys)?
Search WWH ::




Custom Search