Java provides the two ways to create a Thread:

  • Implementing the Runnable interface.
  • Extending the Thread class.

Implementing Runnable interface

The easiest way of creating a thread is to make a class that implements the Runnable interface. The runnable interface represents a unit of executable code. We can create a thread on the object which implements Runnable. A class only implements a single method called run() if it implements the Runnable interface.

We will define the code in the run method of the Runnable interface that constitutes the new thread. The run() can call other methods of the class, can use other classes, and declare variables. The run() establishes the entry point for the execution of the thread within your program. The thread will end when run() finishes.

We can instantiate an object of type Thread in which a class that implements Runnable is passed so that we can start the new thread. Thread has a constructor that we will use to start the run method of the Thread class.

Syntax:

In this constructor, r1 is an instance of a class that implements the Runnable interface, and tname is the name of the new thread. When the new thread is created, it will start running when you call its start() method, which is declared within Thread.

Steps for creating a new thread using Runnable:

Step1: Create a Runnable implemented class and implements the run() method.

Step2: Instantiate Thread class and pass the Runnable implemented class to the Thread, Thread has a constructor which accepts Runnable implementer instance.

Step3: Invoke start() of Thread instance, start()  internally calls run() of the implemented class. By invoking start() creates a new Thread, which executes the code written in the run().

Example1: Creating the thread using the Runnable interface.

Output:

Java Thread Creation

Explanation:

In the Thread2 class ‘s constructor, a new Thread object is created by the following statement:

when start()method calls ,this cause the child thread’s for loop to begin. When the main resume its execution it enters into the loop. Then both sharing the cpu until their loops finish.

Creating the thread by extending the Thread class

A thread class can also make by extending the predefined Thread class, and then to instantiate that class. The entry point run() method for the new thread, must be overridden in the extended class. Here is the preceding program was rewritten to extend Thread.

Steps to create a new thread using Thread class:

Step1: Create a Thread extended class and overriding run() method of the Thread class according to your requirements.

Step2: Instantiate Thread extended class.

Step3: Invoke start() of Thread class through the instance of the Thread extended class, start() internally calls run() method which overrides in the user class. The whole activities of the thread which is defined in the run() executes.

Example1: Create the thread using the Thread class.

Output:

Java Thread Creation2

Explanation:

The thread of the child is created by creating the object of Thread3, which is derived from Thread. The super() inside Thread3 invokes the parent Thread constructor e.g., public Thread(String tName).Here, tName specifies the name of the thread.

Example2: Creating multiple threads and using Thread class having isAlive() and join() method:

It is a program to ensure that the main thread is the last to stop.

Output:

Java Thread Creation3

Explanation:

If we want the main thread to finish last. The program is accomplished by calling sleep() within main(), but how can one thread know when another thread has ended its execution. We can call isAlive() on the thread to determine whether a thread has finished or not. If the thread upon which the isAlive() method is called is still running  it returns true. It returns false if the thread is not running.

The join() method causes to waits the other thread until the thread on which it is called terminates.

Difference between the Thread class and Runnable interface

Following are some key differences between Thread class and Runnable interface:

1. There is an additional method that has to override in the class, i.e., they consume excess or indirect memory, computation time, or other resources.

2. In Java, we can only extend one class, and therefore if we extend Thread class, then we will not be able to extend any other class. That is why we should implement the Runnable interface to create a thread.

3. Runnable makes the code more flexible as, if we are extending a thread, then our code will only be in a thread whereas, in case of runnable, one can pass it in various executor services, or pass it to the single-threaded environment.

4. Maintenance of the code is easy if we implement the Runnable interface.

Pin It on Pinterest

Share This