The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Trail: Essential Java Classes

Lesson: Threads: Doing Two or More Tasks at Once


This chapter has been updated to reflect features and conventions of the latest release, JDK 5.0. If you notice any errors or omissions (or something you really like), please tell us.
A snapshot of three copies of an applet that animates different sorting algorithms is shown in the following figure. No, this chapter is not about sorting algorithms. But these applets do provide a visual aid to understanding a powerful capability of the Java platform — threads.
                  Bidirectional
 Bubble sort       Bubble sort      Quick sort

          

Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the J2SE JRE or JDK. This applet requires JDK 5.0 or later. You can find more information in the Java Plug-in home page.
Start each of the applets, one by one, by clicking on them with the mouse. You can see each one working its way through the data, sorting it, with shorter lines on the top and longer lines on the 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.

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.

What Is a Thread? (in the Essential Java Classes trail)

Using the Timer and TimerTask Classes (in the Essential Java Classes trail)

Subclassing Thread and Overriding run (in the Essential Java Classes trail)

Implementing the Runnable Interface (in the Essential Java Classes trail)

The Life Cycle of a Thread (in the Essential Java Classes trail)

Thread Scheduling (in the Essential Java Classes trail)

Synchronizing Threads (in the Essential Java Classes trail)

Thread Pools (in the Essential Java Classes trail)

Summary (in the Essential Java Classes trail)

Questions and Exercises: Threads (in the Essential Java Classes trail)


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

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