Multithreading in Java

In this tutorial, we will learn about threads and multithreading in Java.
We will learn the following in the following tutorial

  • What are threads?
  • What is multithreading?
  • Some examples on multithreading.

What are Threads?

Threads are a part of a process, while the process is being executed. Every process has at least one thread, which is the ‘Main Thread’. It is created by the Java Virtual Machine(JVM) at the program’s start, when the main() method is invoked.

What is Multithreading in Java

Multithreading is a Java feature that allows more than one thread to run simultaneously from a single program(process). This ensures the maximum utilisation of the CPU. So, threads are light-weight processes within a process.

There are two methods to create a thread –
1. Extend the Thread Class.
2. Implement the Runnable Interface.

Multithreading in Java with Examples

Thread creation by extending the Thread Class

We create a class and that class must extend java.lang.Thread class. Then, we override the run() method. This method is the entry point for the thread(the life cycle begins). To call the run() method, we first create an object of the class and then call the start() method. The start() method invokes the run() method automatically, on the Thread object that is created.

Here is an example program –

class Multithreading extends Thread
{

  public void run()
  {
    try{
      //Display the thread running
      System.out.println("Current thread is: " + Thread.currentThread().getName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}
public class Multithread
{
  public static void main(String[] args)
  {
    int n = 10;
    for(int i=0;i<n;i++)
    {
      Multithreading mlt = new Multithreading();
      mlt.start();
    }
  }

Output – 

Current thread is: Thread-1
Current thread is: Thread-2
Current thread is: Thread-0
Current thread is: Thread-3
Current thread is: Thread-4
Current thread is: Thread-6
Current thread is: Thread-5
Current thread is: Thread-7
Current thread is: Thread-8
Current thread is: Thread-9

Note – Notice that the order might not what you expect it to. It might be different every time you run the program. The order purely depends upon how the CPU schedules the threads, and the order of scheduling is unknown to us.

Thread creation by implementing the Runnable Interface in Java

We create a class and that class must implement java.lang.Runnable class. We override the run() method and then we instantiate a Thread object. Then, we call the start() method.

class Multithreading implements Runnable
{

  public void run()
  {
    try{
      //Display the thread running
      System.out.println("Current thread is: " + Thread.currentThread().getName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}
public class Multithread
{
  public static void main(String[] args)
  {
    int n = 10;
    for(int i=0;i<n;i++)
    {
      Thread t = new Thread( new Multithreading());
      t.start();
    }
  }
}

Output – 

Current thread is: Thread-1
Current thread is: Thread-0
Current thread is: Thread-2
Current thread is: Thread-4
Current thread is: Thread-3
Current thread is: Thread-7
Current thread is: Thread-9
Current thread is: Thread-8
Current thread is: Thread-6
Current thread is: Thread-5

We usually create threads from the second method because according to developers, “A class should be extended only when a new functionality has to be added. Or else, it’s bad practice”. So, the second method is what is used globally and we would suggest you too to use the same.

Similar articles –

Leave a Reply

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