Articles

Explain Multithreading in Java with Example

Share

Another important feature of Java is support for Multithreading programming. It can be viewed as multitasking at the programmatic level. It reduces the program execution time and helps in achieving an efficient program.

In a program, there may be various functionalities which are independent of each other, having unique separate paths. Meaning, these functionalities can be carried out concurrently at the same time span.

This can be achieved through threads, by framing each function or task as a single thread makes parallel execution possible. A thread is nothing, but the smallest executable code without dependency.

Multthreading can be defined as a program having two or more threads performing various independent tasks concurrently

Thread

A thread is the smallest independent part of a program that can execute independently from other threads. It is the smallest piece of executable code that uses shared memory space and other resources with other threads to perform its task.

A single thread called the main() thread can always be found in the newly created program and accountable for the creation and termination of other threads.

Important Notes:

– It utilizes the processor and doesn’t let it sit idle
– Multithreading can be used in a various scenario such as maintaining the responsiveness of application, in the long run, parallel execution of two different tasks, handling client’s request in case of the Web application and more
– It gives  an efficient program
– A thread is also known as lightweight processes

– Thread Priorities play an important role in controlling the thread execution. A thread priority ranges from 1 to 10. 1 represents the lowest priorities and 10 highest. By default, 5 is assigned to a thread as its priority.

Life Cycle of a thread

There are various phases or stages in the lifecycle of a thread. A thread can have multiple states, new, runnable,running, blocked and terminated are the examples. These are the following:-

New: When an instance of a thread is created then it is said to be a New thread is born.

Runnable: When a thread is ready to perform its tasks but the processor hasn’t allocated to it.

Running: When a thread gets the processor and starts executing its tasks.

Blocked: A thread is said to be blocked when it waits for resource or an event to occur.

Terminated: A thread gets terminated when it completes its assigned task or is forcefully stopped. It can’t be resumed from this state.

Click here for a detailed article on Java Thread Lifecycle.

Synchronization

To control asynchronous behavior, synchronization is used. Synchronization can be defined as a scenario where two or more threads try to access a shared resource at the same time. It can be resolved by ‘Monitor’ which can be thought of as a small box having capacity to hold a single thread. So, when a thread gets into the monitor ( area), all other threads require to wait until entered thread performs its task and comes out.

A simple Java program to create a thread

class MyFirstThread extends Thread{

@Override
public void run()
{
    int i;
for(i=0;i<3;i++)
{
System.out.println("I am First Thread");

}

}
}
class MySecondThread extends Thread{
@Override
public void run()
{
    int i;
for(i=0;i<3;i++)
{
System.out.println("I am Second Thread");

}

}
}
public class ThreadProgram {

    public static void main(String[] args) {
 MyFirstThread Thread1 = new MyFirstThread();
  MySecondThread Thread2 = new MySecondThread();
  
 Thread1.start();
 Thread2.start();
    }
    
}

Output:

However, a thread can be created either by implementing Runnable Interface or by extending Thread class. [ Click here to learn]

This post was last modified on June 18, 2020

Sandeep Verma

Published by
Sandeep Verma
Tags: intro java life cycle of a thread program synchronisation threads