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; a thread cannot run on its own. Rather, it runs within a program. The following figure shows this relationship.

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

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 of its own 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. Some 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 API. 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 section Task Scheduling (in the Essential Java Classes trail).

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 provides 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.) These 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 section Implementing the Runnable Interface (in the Essential Java Classes trail) tells you how to do this. The next section, The Life Cycle of a Thread (in the Essential Java Classes trail), discusses how to create, start, and stop a thread. The section Thread Scheduling (in the Essential Java Classes trail) describes how the Java platform schedules threads and how you can intervene in the scheduling.

Since 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 section Synchronizing Threads (in the Essential Java Classes trail) describes how to maintain the integrity of shared resources and how to ensure that each thread has equal access to the resources.

The next section, Thread Pools (in the Essential Java Classes trail), discusses an approach to managing threads that relieves the programmer from worrying about the details of thread life cycles. The chapter concludes with a summary of thread support in the Java language and platform and 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.