Multithreading in Java [Update 2019] – Important Facts That You Should Know

Multithreading, another feature of OOPs, in common words Multithreading in Java helps to execute more than 2 programs simultaneously and we can utilize the capacity of CPU. 

Before we start the Java Multithreading tutorial, you should know about the thread.

1. What is Java Thread

A thread is just a single lightweight process. It is the smallest unit of processing. Applications can have multiple processes. Each of the processes is assigned to a single thread or multiple threads. When these threads work concurrently then this procedure is known as multithreading. A multi-threaded program contains multiple parts that can run simultaneously. Each part of the process is a thread and every thread defines a separate part of the execution.

Is Java An Object-Oriented Language or Not?

Learn the Multithreading Concept with example

2. What is Multithreading in Java?

Multithreading in Java is a process of executing two or more threads simultaneously. To achieve multitasking we use multiprocessing and multithreading. Each thread runs parallel to each other. Although, we use multithreading because the threads use a shared memory area. They don’t allocate separate memory area so it saves the memory. Also, context switching between threads takes less time. Multithreading is mainly used in gaming.

3. Advantages of Multithreading in Java

  • It allows performing multiple operations at once, as they are not dependent on each other, thus not blocking the user.
  • It saves time as multiple operations are possible.
  • They are independent to make the functionality better.

Explore the 8 Astonishing Applications of Java in Real-Time 

Java Threads Processing

Java Quiz

4. How to Achieve Multithreading in Java?

There are two ways to achieve multithreading in Java-

  • By Implementing the Runnable Interface
  • By extending Thread class

4.1 Implementing the Runnable Interface in Java

The simplest way to create a thread is to create a class by implementing the Runnable interface. To do this we need a class which implements a single method called run( ), which is declared like this:

public void run( )

Inside the run( ) method we write the code which will create a new thread.

Example –

package com.dataflair.multithreading;
class MultithreadingDemo implements Runnable
{
  public void run()
  {
    try
    {
      System.out.println ("Thread " +Thread.currentThread().getId() +" is running");
    }
    catch (Exception e)
    {
     System.out.println ("Exception is caught");
     }
      }
}
public class ImplementingRunnableInterface 
{
   public static void main(String args[])
   {
    for (int count=0; count<8; count++)
     {
      Thread object = new Thread(new MultithreadingDemo());
      object.start();
     }
  
    }
}

Output-

implementing-runnable-interface

Do you know how to declare and call a method in Java?

4.2 By extending Thread class

Another way to create a thread is by creating a new class that extends the Thread, then it overrides the run() method and then it creates an instance of that class. The run() method is executed when you call start() method. Here is an example of creating a Java Thread subclass:

Syntax-

public class DataFlair extends Thread 
{
     public void run()
     {
       System.out.println("DataFlair class is running");
     }
}

Example –

package com.dataflair.multithreading;

class MultithreadingDemo1 extends Thread
{
  public void run()
  {
    try
    {
      System.out.println ("Thread " +Thread.currentThread().getId() +" is running");
    }
    catch (Exception e)
    {
      System.out.println ("Exception is caught");
    }
  }
}
public class ExtendingThreadClass
{
  public static void main(String[] args)
  {
    for (int count=0; count<8; count++)
    {
      MultithreadingDemo1 object = new MultithreadingDemo1();
      object.start();
    }
  }
}

Output –Extending-thread-class

5. Life Cycle of a Thread in Java

A thread in multithreading in Java has a predefined lifecycle which it goes under. Following are the various stages under which it goes –

Life Cycle of a Thread in Java

  1. New – A new thread is also called as born thread, it is a new thread that begins its life in a new state, it is not yet started for the execution and is unutilized by the program.
  2. Runnable – When a newly born thread is started and it becomes runnable, it is considered to be in a running state.
  3. Waiting – This state comes when one thread is performing its function and another is waiting for it to perform its function.  The waiting thread will only perform its function when the first thread signals it to do so.
  4. Timed waiting – A thread can come to this state for a limited period of time, i.e. when it goes back to the runnable stage and the time interval expires or when the event for which it was waiting occurs.
  5. Terminated (dead ) – A thread when completes its task, or it is being terminated due to some reasons.

6. Summary

This feature extends the idea of multitasking, here we can divide a particular task within a single application into multiple threads. So, now you know how to achieve multithreading in java and also the life cycle of a thread. This will help you to enter into the gaming industry.

Don’t know what to do next? Learn different types of Java Exceptions

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.