Java Multithreading 101: An Introduction

Multithreading is a feature of java that allows concurrent execution of two or more parts of a program for maximum utilization of CPU.

Each part of such a program is called a thread. So, threads are lightweight processes within a process.

 

 

Ways to create threads

Threads can be created in two ways

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

 

Thread Creation 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 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.

 

// Java code for thread creation by extending
// the Thread class
class MultithreadingDemo 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 Multithread {
    public static void main(String[] args)
    {
        int n = 8; // Number of threads
        for (int i = 0; i < n; i++) {
            MultithreadingDemo object
                = new MultithreadingDemo();
            object.start();
        }
    }
}


Output

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

Thread creation by implementing the Runnable Interface

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

// Java code for thread creation by implementing
// the Runnable Interface
class MultithreadingDemo 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 Multithread {
    public static void main(String[] args)
    {
        int n = 8; // Number of threads
        for (int i = 0; i < n; i++) {
            Thread object
                = new Thread(new MultithreadingDemo());
            object.start();
        }
    }
}


Output

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

 

Thread Class vs Runnable Interface

If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritances. But, if we implement the Runnable interface, our class can still extend other base classes. We can achieve the basic functionality of a thread by extending the Thread class because it provides some inbuilt methods like yield(), interrupt(), etc. that are not available in the Runnable interface. Using runnable will give you an object that can be shared amongst multiple threads.




Post a Comment

0 Comments