Multithreading in Java

Multithreading is one of the most important features of java that allows the execution of multiple threads at the same time.

Java is basically a programming language which is multi-threaded and has the ability to execute two or more processes such that each process can handle their own respective tasks (or operations or functions) on their own.

Multi-threading is generally an extension to multi-tasking. As the name suggests, multi-tasking is the capability to perform different (or multiple) tasks at the same time so that there is proper utilization of resources.

Thread is nothing but the part of the program that is typically light weighted sub-processes. The thread represents a module of an application.

Advantages of MultiThreading over Multi-Processing

1. Because multithreading is based on the concept of threads and threads share a common memory space, so this saves the memory space.
2. Because context switching exists among the multiple threads so it takes less time as compared to processes.

Advantages of Multithreading-

1. Thread is the smallest execution unit which means that each thread is independent of one another so if an exception occurs, it does not affect the other threads.
2. Multithreading allows us to perform any function (or operations) so this saves time.
3. The concept of multithreading uses common memory space that saves the space too.

Life Cycle of JAVA Thread

Different States in the Life Cycle of Thread

1.   New – A Thread is said to be in the new state if an instance (or object) of a thread class is created but before calling the start () method. In this state, a thread is born.

2.   Runnable – After a thread is started, it becomes runnable to execute its tasks, but the thread scheduler does not select it for the next state.

3.   Running – After got selected by the thread scheduler, thread comes to the running state.

4.   Waiting – A time comes when a thread moves to the waiting state and waits for the other thread to perform their specific tasks. A thread then moves back to the runnable state only when the other thread provides the signal for the previously waiting thread to execute its task. In this state, a thread exists but is not allowed to execute (or run). This state is called waiting state.

5.   Dead / Terminated – A thread enters the dead or terminated state when it completes its task or otherwise when the run () method exits.

Note: JVM maintains the Life Cycle of Thread.

Ways to Create a Thread

There are typically two ways to create a thread:

1. By Inheriting / Extending Thread Class.

Example:

output:

 

2. By implementing runnable interface-

Example-

Output-

 

NOTE- In both cases the run() method should be implemented.

Thread Class

A class that provides the necessary method and constructors to create and perform different operations on a thread is called a thread class.

Different Constructor of Thread Class

1. Thread ()
2. Thread (Runnable Target)
3. Thread (Runnable Target, String Name)
4. Thread (String Name)

Methods

1. public void run()– This method is used to perform a particular action for a particular thread.
2. public void start() – This method is used to start the actual execution of the thread. Java virtual machine is responsible for calling the run () method.
3. public void sleep (long millisecond) – This method will allow the currently executing thread to sleep for a specific interval of time (generally millisecond).
4. public boolean isAlive() – This method tests whether the thread is alive or not.

Leave a Comment