Panels

Threads

Thread API

"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

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.

A SimpleThread example:

Because applications go through main() when starting (and Applets through 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.

Examples:

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.

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.

Manipulating 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.


A simple multithread example to test the TestThread:

import java.util.Random;

Runnable

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.
See
Thread API for constructors of Threads

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,
OR
you must pass a Runnable object to the Thread constructor.

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


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

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

Related term:

Useful method: