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

What Is a Thread?

All programmers are familiar with writing sequential programs. You've probably written a program that displays "Hello World!" or sorts a list of names or computes a list of prime numbers; these are sequential programs. That is, each has a beginning, an execution sequence, and an end. At any given time during the runtime of the program, there is a single point of execution.

A thread is similar to the sequential programs described previously. A single thread also has a beginning, a sequence, and an end. At any given time during the runtime of the thread, there is a single point of execution. However, a thread itself is not a program; it cannot run on its own. Rather a thread runs within a program. The following figure shows this relationship.

Thread running within a program.

Thread running within a program.


Definition:  A thread is a single sequential flow of control within a program.

The real excitement surrounding threads is not about a single sequential thread. Rather, it's about the use of multiple threads running at the same time and performing different tasks in a single program. This use is illustrated in the next figure.

Two threads running concurrently in a single program.

Two threads running concurrently in a single program.

A Web browser is an example of a multithreaded application. Within a typical browser, you can scroll a page while it's downloading an applet or an image, play animation and sound concurrently, print a page in the background while you download a new page, or watch three sorting algorithms race to the finish.

Some texts call a thread a lightweight process. A thread is similar to a real process in that both have a single sequential flow of control. However, a thread is considered lightweight because it runs within the context of a full-blown program and takes advantage of the resources allocated for that program and the program's environment.

As a sequential flow of control, a thread must carve out some resources within a running program. For example, a thread must have its own execution stack and program counter. The code running within the thread works only within that context. Other texts use execution context as a synonym for thread.

Thread programming can be tricky, so if you think you might need to implement threads, consider using high-level thread APIs. For example, if your program must perform a task repeatedly, consider using the java.util.Timer (in the API reference documentation) class. The Timer class is also useful for performing a task after a delay. Examples of its use are in the Using the Timer and TimerTask Classes (in the Essential Java Classes trail) section.

If you're writing a program with a graphical user interface (GUI), you might want to use the javax.swing.Timer (in the API reference documentation) class instead of java.util.Timer. Another utility class, SwingWorker, helps you with another common job: performing a task in a background thread, optionally updating the GUI when the task completes. You can find information about both the Swing Timer class and the SwingWorker class in How to Use Threads (in the Essential Java Classes trail).

Basic support for threads in the Java platform is in the class java.lang.Thread (in the API reference documentation). It provides a thread API and all the generic behavior for threads. (The actual implementation of concurrent operations is system-specific. For most programming needs, the underlying implementation doesn't matter.) The behaviors include starting, sleeping, running, yielding, and having a priority.

To implement a thread using the Thread class, you need to provide it with a run method that performs the thread's task. The Implementing the Runnable Interface (in the Essential Java Classes trail) section tells you how to do this. The Life Cycle of a Thread (in the Essential Java Classes trail) section discusses how to create, start, and stop a thread. The Thread Scheduling (in the Essential Java Classes trail) describes how the Java platform schedules threads and how to intervene in the scheduling.

Because threads share a program's resources, it is important to understand how to synchronize access to those resources so that the resources aren't corrupted. The Synchronizing Threads (in the Essential Java Classes trail) section describes how to maintain the integrity of shared resources and how to ensure that each thread has equal access to the resources.

The Thread Pools (in the Essential Java Classes trail) section discusses an approach to managing threads that relieves the programmer from worrying about the details of thread life cycles. This chapter concludes with a summary of thread support in the Java programming language and platform and provides pointers to sources of further information.


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.