Defining a Thread in java using Runnable interface

Defining a thread :

We can define a thread in the following two ways :
a. By extending Thread class
b. By implementing runnable interface.

By implementing runnable interface.

Runnable interface is present in java.lang package and it contains one method called public void run() method.We can pass an object of a class which implements Runnable interface to the constructor a Thread class object and call the Thread class’s start method to start a new child thread, example below.

package com.demo2;


class MyRunnable implements Runnable{

	public void run() {
		IntStream.range(1,10).forEach(x -> System.out.println("Child Thread : "+x));

public class ThreadDemo {
	public static void main(String[] args) {
		Thread t = new Thread(new MyRunnable());
		IntStream.range(1,10).forEach(x -> System.out.println("Main Thread : "+x));

Among the two ways to defining a thread, the implementing Runnable interface approach is suggest because the class implementing Runnable method still has a scope to extend another class but a class extending Thread class cannot support this functionality.

Leave a Comment