Java Reference
In-Depth Information
wristwatch. True multiprocessing allows you to have more than one process running concur-
rently on more than one CPU. Java's support for threading includes multiprocessing, as long
as the operating system supports it. Consult your system documentation for details.
Though most modern operating systems provide threads, Java was the first mainstream pro-
gramming language to have intrinsic support for threaded operations built right into the lan-
guage. The semantics of
java.lang.Object
, of which all objects are instances, includes the
notion of “monitor locking” of objects, and some methods (
notify
,
notifyAll
,
wait
) that
are meaningful only in the context of a multithreaded application. Java also has language
keywords such as
synchronized
to control the behavior of threaded applications.
Now that the world has had years of experience with threaded Java, experts have started
building better ways of writing threaded applications. The Concurrency Utilities, specified in
util.concurrent
package by Professor Doug Lea of the Computer Science Department at
the State University of New York at Oswego. This package aims to do for the difficulties of
threading what the Collections classes (see
Chapter 7
) did for structuring data. This is no
small undertaking, but they pulled it off.
The
java.util.concurrent
package includes several main sections:
▪
Executors
, thread pools, and
Futures
▪
Queues
and
BlockingQueues
▪ Locks and conditions, with JVM support for faster locking and unlocking
▪ Synchronizers, including
Semaphores
and
Barriers
▪ Atomic variables
An implementation of the
Executor
interface is, of course, a class that can execute code for
you. The code to be executed can be the familiar
Runnable
or a new interface
Callable
.
One common kind of
Executor
is a “thread pool.” A
Future
represents the future state of
something that has been started; it has methods to wait until the result is ready.
These brief definitions are certainly oversimplifications. Addressing all the issues is beyond
the scope of this topic, but I do provide several examples.