Multithreading in Java

tutorial and example
tutorial and example

Multithreading is one of the most important feature 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 a proper utilization of resources.

Thread is nothing but the part of the program that are typically light weighted sub-processes. 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 any exception occur it does not affect the other threads.
2. Multithreading allows us to perform many 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:
run:
This an example of multithreading
BUILD SUCCESSFUL (total time: 0 seconds)

2. By implementing runnable interface-

Example-

Output-
run:
This is a concept of Multithreading
Threads are lightweighted
This is a concept of Multithreading
Threads are lightweighted
BUILD SUCCESSFUL (total time: 0 seconds)

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 Targer, String Name)
4. Thread (String Name)

Methods

1. public void run()– This method is use to perform a particular action for a particular thread.
2. public void start() – This method is use to start the actual the execution of the thread. Java virtual machine is responsible to call 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.