Lesson: Doing Two or More Tasks At Once: 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.
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.
Once you know how to get a thread to do something, you need to
understand the life cycle of a Thread
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 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.