English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lifecycle and Thread States in Java

Java is aMultithreading Programming Language, which means we can use Java to develop multithreaded programs. Multithreaded programs contain two or more parts that can run simultaneously, and each part can handle different tasks at the same time, thus fully utilizing available resources, especially when your computer has multiple CPUs.

By definition, multitasking refers to multiple processes sharing common processing resources such as the CPU. Multithreading extends the concept of multitasking to applications where you can break down specific operations within a single application into individual threads. Each thread can run in parallel. The OS not only allocates processing time between different applications but also between threads within an application.

Multithreading allows you to write a way for multiple activities to be performed simultaneously within the same program.

Thread Lifecycle

Threads go through various stages in their lifecycle. For example, threads are born, started, run, and then die. The following diagram shows the complete lifecycle of a thread. The following are the stages of the lifecycle.

  • New-A new thread begins its lifecycle with a new state. It will maintain this state until the program starts the thread. It is also known asBorn Thread.

  • Runnable Interface-A newly born thread enters a runnable state after it is started. Threads in this state are considered to be executing their tasks.

  • Waiting-Sometimes, a thread may enter a waiting state while waiting for another thread to execute a task. The thread will only transition back to a runnable state when the other thread sends a signal to notify the waiting thread to continue execution.

  • Timed Waiting-A runnable thread can enter a timed waiting state within a specified time interval. When the time interval expires or a waiting event occurs, the thread in this state will transition back to a runnable state.

  • Termination (Termination) -A runnable thread enters a terminated state when it completes its task or terminates.