Create/Implement thread/ task using runnable interface & thread class (java/ example)

There are couple of ways to execute dedicated tasks in java.

  • Create a custom thread by sub-classing Thread class
    • Concrete thread class will override the run method of Thread class.
  • We can create a task by implementing Runnable interface.
    • The custom task class needs to implements the run method.
    • An instance of task class can be pass an argument. while creating the Thread.

What is next question comes to our mind? If thread class is there, what is need of Runnable interface?

  1. There is important design principle in place, which states that “Program to an interface, not an implementation”. So, going by the design principle, its recommended to create a task by implementing runnable interface instead of using Thread class.
  2. We should create specialized class, if we are going to modify the feature of its base class. So, if we are extending the thread class for the sake of creating the thread then it is not good design. Moreover, If we have extended the Thread class, then class can not extend any other class (In java, we can extend only one class).

We should refrain from using the thread class to create threads. Its recommended to create a task by implementing the Runnable interface.

In java, we can create thread using following ways.

  1. Create a thread by extending a Thread class.
  2. Create a task by Implementing a Runnable interface.
    1. Pass the instance of task to the thread.
create thread class runnable interface example
Fig 1: Runnable interface & Thread class in java

1. Program: Create a thread by extending a thread class in java.

package org.learn;

public class DomesticHelper extends Thread {
	
	private int salary = 0;

	public void run() {
		System.out.println("2. Perform cooking");
		salary = 50;
		System.out.println("3. Salary is:" + salary);
		System.out.println("4. Perform Cleaning");
		salary += 50;
		System.out.println("5. Salary is:" + salary);
	}

	public static void main(String[] args) throws InterruptedException {
		DomesticHelper worker = new DomesticHelper();
		System.out.println("1. Starting thread");
		worker.start();
		worker.join();
		System.out.println("6. Finished");
	}
}

Output: creating thread by extending a Thread class in java

1. Starting thread
2. Perform cooking
3. Salary is:50
4. Perform Cleaning
5. Salary is:100
6. Finished

2.) Program: Create task by implementing Runnable interface in java

package org.learn;

public class DomesticHelperTask implements Runnable {
	
	private int salary = 0;

	public void run() {
		System.out.println("2. Perform cooking");
		salary = 50;
		System.out.println("3. Salary is:" + salary);
		System.out.println("4. Perform Cleaning");
		salary += 50;
		System.out.println("5. Salary is:" + salary);
	}

	public static void main(String[] args) throws InterruptedException {
		Thread worker = new Thread(new DomesticHelperTask());
		System.out.println("1. Starting thread");
		worker.start();
		worker.join();
		System.out.println("6. Finished");
	}
}

Output: thread execution using task by implementing runnable interface

1. Starting thread
2. Perform cooking
3. Salary is:50
4. Perform Cleaning
5. Salary is:100
6. Finished
Scroll to Top