web analytics

Thread programming is all about dividing the execution of a program into small parallel units so that the execution less time than when it executes line-by-line.

Consider a real world scenario, a user downloading a file from the internet. Without the threads concept, until the file gets downloaded, the user will not be able to do anything except looking at the loading bar in the UI, ie., the program entirely runs on the main thread therefore blocking user to do any other task. By making the downloading file task into a separate thread, the user can carry on other tasks in the main thread. By this approach we can make sure that execution takes less time along with efficient utilization of resources like CPU, memory etc.,

From  the above explanation, A thread can be defined as a smallest unit of running code, which executes separately from the main thread.

Thread programming in Java

In Java, threads can be created in two ways.

  1. By extending Thread class.
  2. By implementing Runnable Interface.

Let’s discuss both the ways. By looking at each method in the end we can conclude which method to use for one’s requirements.

Extending Thread Class

class ChildThread extends Thread{

ChildThread(){

super(“Child Thread is running”);
start();

}

public void run() {

System.out.println(“Hi DaddyThread!!!”);

}

}

public class HelloWorld{

public static void main (String args[]){

System.out.println(“Hi ChildThread!!!”);

new ChildThread();

 

}

}

Output :

Hi ChildThread!!!

Hi DaddyThread!!!

In the above program, we created a ChildThread by extending Thread class. And the ChildThread  is called from the DaddyThread.

Implementing Runnable Interface

class ChildThread implements Runnable{

Thread t;

ChildThread() {

t = new Thread(this);

t.start();

}

public void run() {

System.out.println(“Hi DaddyThread!!!”);

}

}

public class HelloWorld{

public static void main (String args[]){

System.out.println(“Hi ChildThread!!!”);

new ChildThread();

}

}

Output :

Hi ChildThread!!!

Hi DaddyThread!!!

If you observe there is no much difference between the first and second programs. Even though when implementing the Runnable interface we need an instance of Thread class. We can say this as the extra step in the second program, when compared to the first program.

Extending Thread vs Implementing Runnable

1) Java doesn’t support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java.

2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.

3) Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. so logical separation of Task as Runnable than Thread is good design decision.

4) Separating task as Runnable means we can reuse the task and also has liberty to execute it from different means. since you can not restart a Thread once it completes. again Runnable vs Thread for task, Runnable is winner.

5) Java designer recognizes this and that’s why Executors accept Runnable as Task and they have worker thread which executes those task.

6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.

That’s all folks. These are few differences, if you want to add any other, please do comment below.

Happy Coding!!! 🙂

Categories: Java

Leave a Reply

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