---
title: "Multithreading in Java"  
description: "In this article I am telling you about Multithreading in Java"  
author: "Manoj Pandey"  
published: 2015-04-15  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1724/multithreading-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Multithreading in Java

[Concurrency](https://www.mindstick.com/interview/23470/what-is-the-concurrency) is the ability to run several [programs](https://www.mindstick.com/forum/157528/how-exceptions-can-be-handled-in-sql-server-programs) or several parts of a program in parallel. If a time consuming task can be performed asynchronously or in parallel, this improve the [throughput](https://www.mindstick.com/interview/34442/what-is-high-throughput) and the interactivity of the program.\

Java [multithreading](https://www.mindstick.com/articles/11979/multithreading-with-the-backgroundworker-component-in-asp-dot-net) allows you to do [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) tasks at the same time. This is [possible because](https://answers.mindstick.com/qa/39472/the-invention-of-swarm-intelligence-has-been-possible-because-of-which-branche-of-science) modern day computers have multiple CPUs (CPUs are the brain of [your computer](https://www.mindstick.com/articles/13023/choosing-the-best-power-cord-for-your-computer-a-layman-s-guide), and it has a bunch!). One CPU can work on one Thread at a time (unless your CPUs have hyper-[threading](https://www.mindstick.com/blog/784/multithreading-with-c-sharp), in which case it can handle two at a time).

What is Thread-: A thread is light weighted sub process, a smallest unit of processing. Threads are [independent](https://answers.mindstick.com/qa/98599/what-is-the-longest-river-in-the-commonwealth-of-independent-states), if there occurs [exception](https://www.mindstick.com/articles/61/exception-handling-in-c-sharp) in one thread, it doesn't affect other threads. It shares a common memory area.

##### Life Cycle of Thread

![Multithreading in Java](https://www.mindstick.com/mindstickarticle/d2a7fdc6-f433-4b5e-ad8b-53e0b89f2dfc/images/377c114c-e143-402e-a40b-7f0282d15127.png)

New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.

##### Advantages of Multithreading -:

##### \
1. It does not block the user because thread are independent and you can

##### perform multiple operations at same time.

##### \
2. It saved time because we can perform multiple operation at a same time.

##### \
Here I am creating example of Multithread

```
class RunnableDemo implements Runnable {     private Thread t;     private String threadName;      RunnableDemo(String name) {           threadName = name;           System.out.println("Creating " + threadName);     }      public void run() {           System.out.println("Running " + threadName);           try {                for (int i = 4; i > 0; i--) {           System.out.println("Thread: " + threadName + ", " + i);                     // Let the thread sleep for a while.                     Thread.sleep(50);                }          } catch (InterruptedException e) {         System.out.println("Thread " + threadName + " interrupted.");           }           System.out.println("Thread " + threadName + " exiting.");     }      public void start() {           System.out.println("Starting " + threadName);           if (t == null) {                t = new Thread(this, threadName);                t.start();           }     } } public class TestThread {     public static void main(String args[]) {            RunnableDemo R1 = new RunnableDemo("Thread-1");           R1.start();            RunnableDemo R2 = new RunnableDemo("Thread-2");           R2.start();     }}
```

---

Original Source: https://www.mindstick.com/articles/1724/multithreading-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
