Java – Multithreading with Example4 min read

What is Multithreading?

The execution of multiple threads or tasks is called multithreading. The main purpose of multithreading is to achieve the simultaneous execution of more than two tasks so to maximize the use of CPU time.

Thread is a light-weight sub-process or we can say that it is the smallest part of the process. All threads in a program have different execution paths carrying their individual tasks. This is one of the advantages of threads that threads are independent carrying their respective tasks without affecting the others.

Thread Life Cycle:

The life cycle of a thread means the various stages a thread goes during its lifetime.
According to the sun, there are four states. They are: new, runnable, non-runnable, and terminated.

For better understanding, let us consider the running state also.

  • New
  • Runnable
  • Running
  • Non-Runnable (Blocked)
  • Terminated
  • New:
    It is referred to as born thread that is it is in its new state. It won’t do anything until the start() method is invoked.
  • Runnable:
    After the start() method is invoked, the thread will be in its runnable state. But the control is under the scheduler to finish the execution.
  • Running:
    The thread is considered in its running state when it starts executing. The scheduler selects the thread and executes it in the application.
  • Non-runnable:
    In this state, the thread has to wait. When the multiple threads run in an application, one thread has to wait until the other thread gets executed.
  • Terminated:
    After the completion of the process of thread, it is in its dead state or we can say the thread is terminated.

Thread priorities:

In Java thread priorities determines which thread is to execute first and it depends in their priorities.
Context Switching is switching from one state to another.

The higher priority threads are considered to be more important in the application and the operating system executes it first. The range of priorities in java:

  • MIN_PRIORITY (a constant of 1)
  • MAX_PRIORITY (a constant of 10).
  • And default priorities: NORM_PRIORITY (a constant of 5).

Creating a thread:

There are two ways to create a thread:

  • By extending Thread class.
  • By implementing the Runnable interface.

Some of the commonly used Thread class;

  public start(): start a thread by calling its run() method.
  public run(): Entry point for the thread.
  public isAlive(): Determine if a thread is still running.
  public join(): Wait for a thread to terminate.
  public getName(): It is used for Obtaining a thread’s name.
  public getPriority(): Obtain a thread’s priority.
  public sleep(): suspend a thread for a period of time.
  public int getId(): returns the id of the thread.
  public void stop(): is used to stop the thread(deprecated).


1. By extending Thread class.

The thread class provides methods and also constructors for creating and performing some operation on a thread. In this process, we create a class and extend java.lang.Thread class.

  • First, we override the run() method that is present in the thread class and then put the code inside it.
  • Secondly, we create an object for our class and call the start() method for the execution of a thread.

Syntax for Thread class:

Java program of Thread class:

Output:


2. By implementing the Runnable interface:

If one of the class’ instance is intended to be executed by a thread then this can be achieved by implementing a Runnable interface. Implementation of java.lang. Runnable interface has one method called run().

  • Here the first step is to provide a program with a run() method that is to override this method and put the block of code or the logic inside it.
  • Secondly, the Thread object is instantiated and once the thread object is created, we can call the start() method to start the thread which will execute run() method.

Syntax for Runnable interface:

Java program of Runnable interface:

Output:


MORE

Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More