How to create Threads by Implementing Runnable Interface

How to create Threads by Implementing Runnable Interface in Java

In this tutorial, we will discuss,

What are threads?

Difference between Thread and Process.

How to create Threads by Implementing Runnable Interface?

Video Tutorial:

What are Threads…?

Thread is a tiny program running continuously. It is sometimes called a lightweight process. But there lie differences between thread and process.

Difference between Thread and Process.

ThreadProcess
Thread is a lightweight processThe process is a heavyweight process
Thread does not require separate address space for its execution. It runs in the address space of the process to which it belongs.Each process requires a separate address space to execute.

How to make classes Threadable using Thread class?

In Java, we can implement the thread programs using two approaches –

  1. Using Thread class (Click here to read)
  2. Using runnable interface
How to create Threads by Implementing Runnable Interface

As given in the above Fig., there are two methods by which we can write the Java thread programs one is by extending thread class and the other is by implementing the Runnable interface.

See also  Difference between Class & Interface Extend Implement Interface

1. The run() method is the most important method in any threading program. By using this method the thread’s behavior can be implemented. The run method can be written as follows –

public void run()
{
     Statement for implementing thread
}

2. For invoking the thread’s run method the object of a thread is required. This object can be obtained by creating and initiating a thread using the start() method.

Reasons to create threads by Implementing Runnable Interface vs by extending Threads class

The thread can also be created using the runnable interface. Implementing thread program using Runnable interface is preferable to implementing it by extending the thread class because of the following two reasons –

  1. If a class extends a thread class then it can not extends any other class which may be required to extend.
  2. If a class thread is extended then all its functionalities get inherited. This is an expensive operation.

The simple Java Program to how to create Threads by Implementing Runnable Interface?

class MyThread implements Runnable 
{ 
	public void run() 
	{ 
		System.out.println("Thread is created!");
	} 
} 

class ThreadProgRunn 
{ 
	public static void main(String args[]) 
	{
		MyThread obj=new MyThread(); 
		Thread t=new Thread(obj); 
		t.start();
	}
}

Output:

Thread is created!!!

See also  Java program to find area of rectangle triangle Interface

Program Explanation:

In the above program, we have used the interface Runnable. While using the interface, it is necessary to use implements keyword. Inside the main method creates the instance of class MyThread. This instance is passed as a parameter to the Thread class. Using the instance of class Thread invokes the start method. The start method in turn calls the run method written in MyThread. The run method executes and displays the message for thread creation.

Summary:

This tutorial discusses, How to create Threads by Implementing Runnable Interface in Java. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *