"Imagine that you are using your favorite text editor on a large file. When it starts up, does it need to examine the entire file before it lets you edit? Does it need to make a copy of the whole file? Wouldn't it be nice for it to show you the first page, enabling you to begin editing, and somehow (in the background) complete the slower tasks necessary for initialization? Threads allow exactly this kind of within-the-program parallelism." from Java in 21 Days
SUN tutorial and What is a Thread?
A thread represents a single process in execution on a system. Threads are sometimes referred to as light-weight processes or execution contexts. Typically, each thread controls a single aspect within a program, such as monitoring a particular input device or handling all of the disk I/O.
Has an execution context, program counter, stack, ...
A single-threaded program uses only one thread to control its execution. Many
applications and applets are single-threaded.
If you see a browser page with two applets running at the same time, or
scrolled your page while the applet continued running, then the browser
is multithreaded.
Multithreaded applications (and applets) use several execution contexts to
accomplish their tasks. They make use of the fact that many tasks contain
separate and distinct sub-tasks. One thread can be used for each subtask.
A multithreaded program allows each thread to start and finish as quickly as
possible; this behavior provides better response to real-time input.
Note also that the vonNeumann machines will not be here forever; multiple
processor machines are clearly here/coming; this is great
experience in parallel processing and parallel algorithm considerations.
Here are a couple of examples of SUN using Threads for use in Swing:
How to Use Threads and Threads in Applets
A SimpleThread example:
Because applications go through main() when starting (and Applets through
Examples:
Because we have direct control of our thread, we must start it directly as
well:
t1.start();
Below is a first simple use of threads.
t should contain a valid thread of execution. We control this thread in the
run() method.
Once inside the run methood, you can execute statements as in any program.
run() serves as the main() routine for threads. When
run() finishes, so does the thread.
In these examples, we are pausing for a period of time (random (below) and 300
milliseconds (above))
sleep (300);
The sleep() method simply tells a thread to be quiet for (at least) the
specified number of milliseconds. You can use sleep when you delay execution
of a thread. The sleep() method does not take up system resources while the
thread sleeps. Other threads can (will) continue to work.
Below is a another simple introduction to threads.
import java.util.Random;
The other way to create a thread is to declare a class that implements the Runnable interface.
That class then
implements the run method. An instance of the class can then be allocated, passed as an argument when
creating Thread, and started.
The program below creates a thread that prints 3 messages.
Notice the interface Runnable (remember interface and
implement?)
To create a thread, you must subclass Thread so that it defines its
own run() method,
The interface specifies the run() method that is required for use with
the Thread class. Any class that implements this interface can
provide the "body" of a thread. By implementing the interface
Runnable you declare your intention to run on a separate thread.
See notes on methods in Thread that were deprecated...and why
Related term:
Introducing Threads
SO, threads can be simple but also possibly dangerous.
First, let us look at the basics. There are two main techniques with which to
instantiate threads one uses inheritance and the other implements an Interface.
Then we will consider some of the problems.
init()), note that with Swing, they
start the programs off with calls to Runnable objects that immediately define and call the Interfaces run() method.
Here are examples of instantiating classes that extend Thread. Look at the classes'
(T , TestThread , SimpleThread) constructors for more information.
The first line creates a new subclass of thread called T. In the
second, the two arguments passed to TestThread (see below) represent
the name of our thread and the time we want the thread to delay before printing
its message.
Manipulating Threads
While the main() method retrieves its arguments from the argv parameter (which is typically set from the command line), the newly created thread must receive its arguments programmatically from the originating thread.
Hence, parameters can be passed in via the constructor, static variables,
or any other technique designed by the developer
A simple multithread example to test the TestThread:
Runnable
See Thread API for constructors of Threads
OR
you must pass a Runnable object to the
Thread constructor.
Particular help for CSCI 311 Course
Thread: a shorthand for thread of control: a section of code executed
independently of other threads of control within a single program. API
"A thread is an independent flow of control within a process, composed of
a context (which includes a register set and a program counter) and a sequence
of instructions to execute. An independent flow of control is essentially
an execution path through the program code in a process. The register set
and program counter contain values that indicate the current state of program
execution. Finally, the sequence of instructions to execute is the actual program code"
Theoretical States of a thread
Since problems were seen in the exact state of a thread, implementation specific states have now evolved to NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED and in java 1.5 we have a nested class Thread.State and Thread.getState(). For specifics, see the
Thread.State API
A thread object is in the initial state from the period when it is created
(that is, its constructor is called) until the start() method of
the thread object is called
A thread is in the runnable state once its start() method has been
called. There are various ways in which a thread leaves the runnable state,
but the runnable state can be thought of as a default state: if a thread
isn't in any other state, it's in the runnable state
A thread that is blocked is one that cannot be run because it is waiting for
some specific event to occur. A simple example is the case of a thread that
has opened a socket to some remote data server and attempts to read data from
the server when data is not available.
A thread is in the exiting state once its run() method returns or
its stop() method has been called
Useful method:
No matter how we could have coded the join() loop, the time to
complete the join() will be the time it takes for the last thread to
finish. Still confused? Here is more on join().