Creation of a thread


A thread can be created in two ways
a) By implementing the Runnable interface. The Runnable interface consists of only one method - the run method. The run method has a prototype of
public void run();
b) By extending the class Thread.

1) Implementing the Runnable Interface

The Runnable Interface Signature

public interface Runnable {

void run();

}

One way to create a thread in java is to implement the Runnable Interface and then instantiate an object of the class. We need to override the run() method into our class which is the only method that needs to be implemented. The run() method contains the logic of the thread.

The procedure for creating threads based on the Runnable interface is as follows:

1. A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object.

2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.

3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned.

4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.

Below is a program that illustrates instantiation and running of threads using the runnable interface instead of extending the Thread class. To start the thread you need to invoke the start() method on your object.

public class ThreadExample2 implements Runnable {
public void run() {
.../* Code which gets executed when
thread gets executed. */
}
public static void main(String args[]) {
ThreadExample2 Tt = new ThreadExample2();
Thread t = new Thread(Tt);
t.start();
}
}

Example - Creating thread by implementing Runnable


2) Extending Thread Class

The procedure for creating threads based on extending the Thread is as follows:

1. A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.

2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.

3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.

Below is a program that illustrates instantiation and running of threads by extending the Thread class instead of implementing the Runnable interface. To start the thread you need to invoke the start() method on your object.

public class ThreadExample extends Thread {
public void run() {
System.out.println("Thread started");
}
public static void main(String args[]) {
ThreadExample t = new ThreadExample();
t.start();
}
}

Example - Creation of Thread by extending the
Thread class.

Related Post:-

People who read this post also read :



4 comments:

Note, since JDK 1.5, the recommended way is to use Executors, rather than Threads directly:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html

In the above post threads are very well described , the way you have explained the procedure and provided a good example makes it easier for learners to understand it well. Good work.

sap ecc 6

Thanks tani.................

This is a very clearly written and helpful post - thanks so much!!

Another good reference on same subject of threads in Java:

create thread in Java

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More