View RSS Feed

My Java Tips

Creating a Thread (implementing Java Runnable Interface)

Rate this Entry
by , 11-12-2011 at 05:58 PM (5208 Views)
A better way to create a thread in Java is to implement Runnable interface. A thread can be created by extending Java Thread class also. Now the question arises why implementing Runnable interface is a better approach? Answer is, if the thread class you are creating is to be subclass of some other class, it canít extend from the Thread class. This is because Java does not allow a class to inherit from more than one class. In such a case one can use Runnable interface to implement threads.


Let us see an example of creating a thread by implementing Runnable interface.

Java Code:
class PrintString {
	public static void main(String args[]) {
		StringThread t = new StringThread("Java", 50);
		new Thread(t).start();
	}
}

class StringThread implements Runnable {
	private String str;

	private int num;

	StringThread(String s, int n) {
		str = new String(s);
		num = n;
	}

	public void run() {
		for (int i = 1; i <= num; i++)
			System.out.print(str + " ");
	}
}
Output:


Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

If you have had created a thread class by extending Thread class, you could have directly called start() method as t.start (), where t is a thread object. This is because thread class created by extending Thread class is a subclass of Thread class, so it has all functionalities of a thread. While creating a thread implementing Runnable, a Thread object will have to be explicitly created which is what PrintString class is doing. It then passes StringThread object as a parameter to this thread and runs it. This causes the run ( ) method of StringThread class to get executed.

The above program can be rewritten, by implementing Runnable interface, and creating a Thread object inside StringThread class rather than in the PrintString class as below.

Java Code:
class PrintString {
	public static void main(String args[]) {
		new StringThread("Java", 50);
	}
}

class StringThread implements Runnable {
	private String str;

	private int num;

	StringThread(String s, int n) {
		str = new String(s);
		num = n;
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {
		for (int i = 1; i <= num; i++)
			System.out.print(str + " ");
	}
}
Output:


Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java

Submit "Creating a Thread (implementing Java Runnable Interface)" to Facebook Submit "Creating a Thread (implementing Java Runnable Interface)" to Digg Submit "Creating a Thread (implementing Java Runnable Interface)" to del.icio.us Submit "Creating a Thread (implementing Java Runnable Interface)" to StumbleUpon Submit "Creating a Thread (implementing Java Runnable Interface)" to Google

Comments