The JavaTM Tutorial

| Start of Tutorial > Start of Trail | Search Feedback Form

Trail: Essential Java Classes

## Lesson: Threads: Doing Two or More Tasks At Once

> Below are three copies of an applet that animates different sorting algorithms. No, this lesson is not about sorting algorithms. But these applets do provide a visual aid to understanding a powerful capability of the JavaTM platform--threads.

>                         Bi-Directional
>     Bubble Sort         Bubble Sort        Quick Sort

> Now start each of the applets, one by one, by clicking on them with the mouse. You can see each applet working its way through the data, sorting it, with shorter lines on top and longer lines on bottom. While the applets are sorting, also notice that you can scroll the page or bring up one of your browser’s panels. All this is due to threads.

### What Is a Thread?

> A thread--sometimes called an execution context or a lightweight process--is a single sequential flow of control within a program. You use threads to isolate tasks. When you run one of these sorting applets, it creates a thread that performs the sort operation. Each thread is a sequential flow of control within the same program (the browser). Each sort operation runs independently from the others, but at the same time.

### Using the Timer and TimerTask Classes

> Thread programming can be tricky. Whenever possible, you should use high-level thread API such as the `java.util.Timer` class introduced in version 1.3 of the Java platform. `Timer` and its companion class, `TimerTask`, are useful when your program must perform a task repeatedly or after a delay.

### Customizing a Thread's run Method

> Basic support for threads in all versions of the Java platform is in the `java.lang.Thread` class. It provides a thread API and all the generic behavior for threads. 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 Life Cycle of a Thread

> Once you know how to get a `Thread` to do something, you need to understand its life cycle.

### Understanding Thread Priority

> A thread's priority affects when it runs in relation to other threads. This section talks about how this affects your programs.

### Synchronizing Threads

> The first sample programs in this lesson use either one thread or multiple threads that run asynchronously. However, it is often useful to use multiple threads that share data and therefore must synchronize their activities. In this section you will learn how to synchronize threads and how to avoid problems such as starvation and deadlock.

### Grouping Threads

> This section shows you how to group threads and what you can do with a group of threads.

### Summary

> When you've completed this lesson on threads, you will have toured the intricacies of threads in the Java platform, including the life cycle of a thread (as represented by its state), scheduling, thread groups, and synchronization. The Java platform supports multithreaded programs through the language, the libraries, and the runtime system. This summary page highlights the features in the Java platform that support threads and gives you links to further documentation about those features.

| Start of Tutorial > Start of Trail | Search Feedback Form

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