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

Synchronizing Threads

So far the examples in this chapter have contained independent, asynchronous threads. Each thread has all the data and methods required for its execution and doesn't require any outside resources or methods. Also, the threads in the examples run at their own pace without concern for the state or activities of any concurrently running threads.

However, in many interesting situations, separate, concurrently running threads do share data and must consider the state and activities of other threads. In one such set of programming situations, called producer-consumer scenarios, the producer generates a stream of data that a consumer uses.

For example, imagine an application in which one thread (the producer) writes data to a file while a second thread (the consumer) reads data from the same file. Or, as you type characters on the keyboard, the producer thread places mouse events in an event queue and the consumer thread reads the events from the same queue. Both use concurrent threads that share a common resource: the first shares a file; the second shares an event queue. Because the threads share a common resource, they must be synchronized.

The next section teaches you about thread synchronization by using a simple producer-consumer example.


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.