The JavaTM Tutorial

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

Trail: Essential Java Classes

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

> 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 Java language--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. Notice anything? Yes! The applets are running side by side at the same time! Notice anything else? Yes! You can also scroll this page or bring up one of your browser's panels at the same time that the three sorting applets sort their data. All of this is due to the power of 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.

### Customizing a Thread's `run` Method

> First, you need to get a thread to do something by providing the `run` method for a thread. This section shows you two different ways to do this.

### The Life Cycle of a Thread

> Once you know how to get a thread to do something, you need to understand the life cycle of a `Thread`

### 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 Java threads including the life cycle of a Java thread (as represented by its state), scheduling, thread groups, and synchronization. The Java development environment supports multithreaded programs through the language, the libraries, and the runtime system. This summary page highlights the features in the Java development environment that support threads and gives you links to further documentation about those features.

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