Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This chapter provides a great deal of information about using threads in the Java platform. This section summarizes where you can find various classes, methods, and language features that relate to threads.
Thejava.lang
package provides basic support for threads with the following classes and interfaces:The
- The
Thread
class defines and implements threads. You can subclass theThread
class to provide your own thread implementations.- The
Runnable
interface allows any class to provide the body (therun
method) for a thread.- The root class,
Object
, defines three methods you can use to synchronize methods around a condition variable:wait
,notify
, andnotifyAll
.java.util.concurrent.*
packages define a wide range of concurrency utilities including:
- Task scheduling framework: The
Executor
framework is a collection of interfaces and classes for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Implementations are provided that allow tasks to be executed within the submitting thread, in a single background thread (as with events in Swing), in a newly created thread, or in a thread pool, and developers can create implementations ofExecutor
supporting arbitrary execution policies. The built-in implementations offer configurable policies such as queue length limits and saturation policy which can improve the stability of applications by preventing runaway resource consumption.
- Locks: While locking is built into the Java programming language via the
synchronized
keyword, there are a number of inconvenient limitations to built-in monitor locks. Thejava.util.concurrent.locks
package provides a high-performance lock implementation with the same memory semantics as synchronization, but which also supports specifying a timeout when attempting to acquire a lock, multiple condition variables per lock, non-lexically scoped locks, and support for interrupting threads which are waiting to acquire a lock.
- Synchronizers: General purpose synchronization classes, including semaphores, mutexes, barriers, latches, and exchangers, which facilitate coordination between threads.
- Concurrent collections: The Collections Framework (discussed in Collections, contains several concurrent collections, including the
Queue
andBlockingQueue
interfaces, and high-performance, concurrent implementations ofMap
,List
, andQueue
.
- Atomic variables: Classes for atomically manipulating single variables (primitive types or references), providing high-performance atomic arithmetic and compare-and-set methods. The atomic variable implementations in
java.util.concurrent.atomic
offer higher performance than would be available by using synchronization (on most platforms), making them useful for implementing high-performance concurrent algorithms as well as conveniently implementing counters and sequence number generators.
- Nanosecond-granularity timing: The
System.nanoTime
method enables access to a nanosecond-granularity time source for making relative time measurements, and methods which accept timeouts (such as theBlockingQueue.offer
,BlockingQueue.poll
,Lock.tryLock
,Condition.await
, andThread.sleep
) can take timeout values in nanoseconds. The actual precision ofSystem.nanoTime
is platform-dependent.
The Java programming language has two keywords related to the synchronization of threads:volatile
andsynchronized
. Both of these language features help ensure the integrity of data that is shared between two concurrently running threads. The section Synchronizing Threads discusses thread synchronization issues.
The Java runtime environment contains the scheduler, which is responsible for running all the existing threads. The scheduler uses a fixed-priority scheduling algorithm, which usually means that at any given time, the highest-priority thread is running. However, this is not guaranteed. The thread scheduler may choose to run a lower-priority thread to avoid starvation. For this reason, use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.
This chapter has only scratched the surface on the topics of threads and concurrency control. For further information, see:
- Java Threads, Third Edition, Scott Oaks and Henry Wong, (O’Reilly, 2004)
- Brian Goetz's articles.
- For further information about threads programming, see Doug Lea's highly acclaimed book, Concurrent Programming in Java, Second Edition, written for intermediate and advanced threads programmers.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.