Open In App

Multithreading in Java

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Multithreading is a Java feature that allows the concurrent execution of two or more parts of a program for maximum utilization of the CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process.

Different Ways to Create Threads

Threads can be created by using two mechanisms:

  1. Extending the Thread class 
  2. Implementing the Runnable Interface

1. By Extending the Thread Class

We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside the run() method. We create an object of our new class and call the start() method to start the execution of a thread.start() invokes the run() method on the Thread object.

Example: Creating a Thread by extending the Thread class.

Java
// Java code for thread creation by extending
// the Thread class
class Multithreading extends Thread {
    public void run()
    {
        try {
            // Displaying the thread that is running
            System.out.println(
                "Thread " + Thread.currentThread().getId()
                + " is running");
        }
        catch (Exception e) {
            
            // Throwing an exception
            System.out.println("Exception is caught");
        }
    }
}

// Main Class
public class Geeks
{
    public static void main(String[] args)
    {
        int n = 8; // Number of threads
        for (int i = 0; i < n; i++) {
            Multithreading object
                = new Multithreading();
            object.start();
        }
    }
}

Output
Thread 13 is running
Thread 14 is running
Thread 12 is running
Thread 11 is running
Thread 16 is running
Thread 15 is running
Thread 18 is running
Thread 17 is running


2. By Implementing the Runnable Interface

We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a Thread object and call start() method on this object. 

Example: Demonstrating the multithreading by implementing the Runnable interface.

Java
// Java code for thread creation by implementing
// the Runnable Interface
class Multithreading implements Runnable {
    public void run()
    {
        try {
            // Displaying the thread that is running
            System.out.println(
                "Thread " + Thread.currentThread().getId()
                + " is running");
        }
        catch (Exception e) {
            
            // Throwing an exception
            System.out.println("Exception is caught");
        }
    }
}

// Main Class
class Geeks {
    public static void main(String[] args)
    {
        int n = 8; // Number of threads
        for (int i = 0; i < n; i++) {
            Thread object
                = new Thread(new Multithreading());
            object.start();
        }
    }
}

Output
Thread 12 is running
Thread 15 is running
Thread 18 is running
Thread 11 is running
Thread 17 is running
Thread 13 is running
Thread 16 is running
Thread 14 is running


Thread Class vs Runnable Interface

Aspect

Thread Class

Runnable Interface

Inheritance

We need to extend a Thread class. Because it is a class. It means we can not add any other class if we extend it.

It is a interface so use using the implent keyword and allows us to use other classes as well.

Thread Sharing

The Thread object cannot be sharing among the different multiple threads.

The object of Runnable interface can be shared among different threads.

Implementation

We can override the run() method present in the Thread class.

We can implement the run() method present in the Runnable interface.

Built-in Methods

It provides methods like yield(), interrupt() and getId etc.

These methods are not present in the Runnable interface .



Next Article
Article Tags :
Practice Tags :

Similar Reads