站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Summary - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Threads: Doing Two or More Tasks at Once

Summary

This chapter provides a great deal of information about using threads in the Java platform. This section provides information about where to find various classes, methods, and language features that relate to threads.

Package Support for Threads

The java.lang package provides basic support for threads with the following classes and interfaces:
  • The Thread class defines and implements threads. You can subclass the Thread class to provide your own thread implementations.
  • The Runnable interface allows any class to provide the body (the run method) for a thread.
  • The root class, Object (in the API reference documentation), defines three methods you can use to synchronize methods around a condition variable: wait, notify, and notifyAll.
The java.util.concurrent.* packages define a wide range of concurrency utilities, including the following:
  • 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; developers can create implementations of Executor supporting arbitrary execution policies. The built-in implementations offer configurable policies, such as queue-length limits and saturation policy, that 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. The java.util.concurrent.locks package provides a high-performance lock implementation with the same memory semantics as synchronization; it 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 waiting to acquire a lock.

  • Synchronizers: General-purpose synchronization classes, including semaphores, mutexes, barriers, latches, and exchangers, facilitate coordination between threads.

  • Concurrent collections: The Java Collections Framework (discussed in Collections (in the Essential Java Classes trail), contains several concurrent collections, including the Queue and BlockingQueue interfaces, and high-performance, concurrent implementations of Map, List, and Queue.

  • Atomic variables: Classes for atomically manipulating single variables (primitive types or references) provide 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 that accept timeouts, such as BlockingQueue.offer, BlockingQueue.poll, Lock.tryLock, Condition.await, and Thread.sleep, can take timeout values in nanoseconds. The actual precision of System.nanoTime is platform-dependent.

Language Support of Threads

The Java programming language has two keywords, volatile and synchronized, related to the synchronization of threads. Both of these language features help ensure the integrity of data that is shared between two concurrently running threads. The Synchronizing Threads (in the Essential Java Classes trail) section discusses thread synchronization issues.

Runtime Support of Threads

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.

Other Thread Information

This chapter has only scratched the surface on the topics of threads and concurrency control. For further information, refer to the following:
  • Java Threads, Third Edition, Scott Oaks and Henry Wong (O'Reilly, 2004)
  • Java Concurrency in Practice (outside of the tutorial), Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea (Addison Wesley, available early 2006)
  • For further information about threads programming, see Doug Lea's highly acclaimed book, Concurrent Programming in Java, Second Edition (outside of the tutorial), written for intermediate and advanced threads programmers.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.