Start of Tutorial > Start of Trail |
Search
Feedback Form |
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 SortNow 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.
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.
Thread programming can be tricky. Whenever possible, you should use high-level thread API such as thejava.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.
Basic support for threads in all versions of the Java platform is in thejava.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 theThread
class, you need to provide it with arun
method that performs the thread’s task.
Once you know how to get a Thread
to
do something, you need to understand its life cycle.
A thread's priority affects when it runs in relation to other threads. This section talks about how this affects your programs.
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.
This section shows you how to group threads and what you can do with a group of threads.
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 |