Customizing a Thread's run Method

The run method gives a thread something to do. Its code implements the thread's running behavior. It can do anything that can be encoded in Java statements: compute a list of prime's, sort some data, perform some animation.

The Thread class implements a generic thread that, by default, does nothing. That is, the implementation of its run method is empty. This is not particularly useful, so the Thread class defines API that lets a Runnable object provide a more interesting run method for a thread.

There are two techniques for providing a run method for a thread:

·         Subclassing Thread and Overriding run

Examples:

Processus.java, implementation of a Thread using inheritance.
TestProc.java, main class used for testing.

 

 

·         Implementing the Runnable Interface

Examples:

ThreadRunnable.java, implementation of a Thread without using inheritance.
TestRunnable.java, main class used for testing.

 

 

Hosted by www.Geocities.ws

1