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.
- The custom task class needs to implements the
What is next question comes to our mind? If thread class is there, what is need of Runnable interface?
- 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.
- 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.
- Create a thread by extending a Thread class.
- Create a task by Implementing a Runnable interface.
- Pass the instance of task to the thread.
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