Creating Threads (extending Thread class vs implementing Runnable interface)

Creating Threads (extending Thread class vs implementing Runnable interface)

A thread in the Java programming language is a lightweight multi-processing sub-process task or method that allows a program created in Java to execute multiple thread tasks simultaneously. Java developers have built-in support for multithreaded thread task processing through the Thread class and Runnable interface.

Creating Threads (extending Thread vs implementing Runnable interface)

There are two common ways to create multiple threads in Java.

  • This is done by extending the Thread class.
  • Implementing the Thread class by implementing the Runnable interface.

Creating a thread in Java by extending the Thread class.

Basic Thread class concept.

The Thread class in the Java programming language is a built-in or built-in feature of the java.lang package. Java developers can create their own custom threads by developing a class that extends the Thread class.

A Java user-developed thread that executes a user-defined task is created or defined within Java’s run() function method.

When a Java developer calls the start() function method for a thread, Java creates a new thread, and the existing run() function or method in the thread is executed by that thread.

Syntax of the basic Thread class concept.

class SampleThread extends Thread {

public void run() {

// Here we create a thread task to be executed

}

}

Example of the basic Thread class concept.

class SampleThread extends Thread {

public void run() {

System.out.println(“Sample Thread is continuing”);

}

}

public class Main {

public static void main(String[] args) {

SampleThread th1 = new SampleThread();

th1.start();

}

}

Illustrating the basic Thread class concept with this example.

  • In this example, SampleThread extends a user-defined Thread class.
  • Java users override this via a run() function method.
  • The run() function method defined in Thread defines a user-created task to be performed by the thread.
  • Where th1 is an object of SampleThread.
  • It starts a new thread in a th1.start() function method.
  • The JVM in Java then executes the run() function method.

A key point in Java thread development: start() vs. run().

Java developers should use this in general:

th1.start();

and not.

th1.run();

Here, the start() function method in the Thread class creates a new thread, while directly calling the run() function method executes the method in the existing thread like a normal method call.

Creating a thread by implementing the Runnable method interface in Java.

Basic Thread Runnable concept.

Another way to develop a new thread design in the Java programming language is to implement a thread through the Runnable interface.

A user-defined Thread class contains a single abstract method defined in the Runnable interface.

public void run()

In this, Java users implement this method and define the task that needs to be performed in the current thread.

Then, a thread object is created by passing the Runnable method object to its constructor method.

Syntax of the Thread Runnable.

class SimpleTask implements Runnable {

public void run() {

// This defines the task to be created and executed in the current thread.

}

}

Example of the Thread Runnable concept.

class SimpleTask implements Runnable {

public void run() {

System.out.println(“User-defined Thread is running”);

}

}

public class Main {

public static void main(String[] args) {

SimpleTask task = new SimpleTask();

Thread th1 = new Thread(task);

th1.start();

}

}

Here’s the Thread Runnable concept in a Java program.

  • In this program, SimpleTask creates a new thread implementing the Runnable interface.
  • Java users override a run() function method in an existing program.
  • A user-defined task creates an object of SimpleTask.
  • Java users pass the task to the Thread constructor element.
  • Where the th1.start() function method starts the thread.
  • Where the thread executes the run() function method.

The essential point of the Thread Runnable concept is this.

In the above example, `This` represents a Runnable task, while `Thread` represents the thread that executes the thread task.

Main Difference Between Java Thread and Runnable Method

#Extending thread in javaImplementing runnable in java
1.Here we used class extends threadIn this we use class implements runnable
2.Example: class employee extends with threadExample: class company implements runnable thread task
3.In this the class itself represents a thread and its dataIn this the runnable class represents a thread and its task
4.Thread class cannot extend another classBut runnable thread can be extended with another class
5.Java thread is less flexible for useJava runnable method more flexible in term of use
6.Java class thread object is created directly by userWhere java runnable thread task object is passed to thread element
7.Java thread stat with new employee().start()Java runnable thread new thread(new company()).start()

Why is Runnable generally preferred in Java?

Java programs do not support the concept of multiple inheritance of classes.

As we already have an example here,

class Company extends Employee {

}

Java users cannot do this.

class Company extends Employee, Thread { // Display Error

}

But Java users can implement the Runnable thread method.

class Company extends Employee implements Runnable {

public void run() {

System.out.println(“Company thread Created”);

}

}

Because of this, implementing the Runnable thread method in this example gives Java users more flexibility.

It also manages two different responsibilities in Java.

  • Runnable → It indicates in the Java thread program which thread the task should be performed.
  • Thread → In a Java thread program, it indicates which thread will complete the task.

A simple example of creating and managing a real-life thread in a Java program.

So, let’s understand a thread task in the Java programming language using the example of a restaurant.

  • Runnable = This is a task/order to be completed in a restaurant.
  • Thread = This is a worker or employee performing multiple tasks in that restaurant.

For a restaurant thread task example.

class ServingTask implements Runnable {

public void run() {

System.out.println(“Perform ServingTask…”);

}

}

The task is to serve guests with different tasks.

After this.

ServingTask task = new ServingTask();

Thread staff = new Thread(task);

staff.start();

Here, the ServingTask tells the staff employee in a restaurant what to do, while the Thread provides the staff employee who will do it.

The essential methods for implementing a thread in a restaurant.

run() function method.

The run function method in a Java thread contains the source code of the user-defined thread on which your thread runs.

public void run() {

System.out.println(“This is a Test thread”);

}

start() function method.

This starts a new thread in the Java program and runs the run() function method in that new thread.

th1.start();

sleep() function method.

This function puts the currently running thread in the current program to sleep or stop for a certain period of time.

Thread.sleep(2000);

This stops the thread in the current program for approximately 2000 milliseconds (2 seconds).

Conclusion of creating threads (extending the Thread class vs. implementing the Runnable interface).

The Java programming language provides two popular ways to create a new thread.

Using Thread in Java.

class SampleThread extends Thread {

public void run() {

System.out.println(“User Define Thread Running”);

}

}

Using Runnable in Java Threads.

class SampleTask implements Runnable {

public void run() {

System.out.println(“User Define Thread Task Is Running”);

}

}

In most Java thread use cases, applying the Runnable concept is the best choice because it maintains a good separation between the task defined in the Java program and the thread, and allows a class to extend another class.

An easy way to remember the thread concept in Java.

  • Thread → “I am a thread running a user-defined Java program.”
  • Runnable → “I am a user-defined task in a Java program that can run a Java user-defined thread.”

Leave a Reply