Anji…
4 min readNov 16, 2024

Multithreading plays a crucial role in any programming language. Being completely aware of them enables you to build the most performant applications. I would like to write a series of articles to provide a deep dive into Java threads, starting from beginner to expert level. In this article, we will do a deep dive basic overview of threads and their lifecycle.

In general, a thread refers to the smallest unit of execution in a program. Before delving into the topic, let us understand the various thread types referred to in the general context of programming.

Thread Types:

  • Thread
  • Platform Thread
  • OS Thread

The below table provides a comprehensive overview of the thread types.

What is a thread?

A thread in programming is the smallest unit of execution within a process. It represents a single sequence of instructions that a CPU can execute. Threads allow a program to perform multiple tasks simultaneously, enabling efficient use of system resources and improving performance in multitasking environments.

Typically, every program starts with a single thread, known as the main thread, which is provided by the Java Virtual Machine (JVM) or Common Language Runtime (CLR) in .NET at the beginning of the program’s execution. The main thread is responsible for invoking the main() method, serving as the entry point for the program.

Key Characteristics of Threads

  1. Shared Memory: Threads within the same process share the process’s memory space (e.g., heap), which makes communication between threads faster. However, it also introduces risks like race conditions.
  2. Independent Execution: Each thread runs independently, following its own path of execution, though it can be paused, resumed, or stopped by the operating system or program logic.
  3. Lightweight: Threads are lighter than processes because they share memory and resources. Starting a thread consumes fewer system resources compared to starting a new process.
  4. Concurrent Execution: Multiple threads can run concurrently, either on a single CPU core (via context switching) or on multiple cores (parallelism).

Threads Advantages

  • Parallelism: Multiple tasks can run simultaneously, improving program efficiency on multi-core systems.
  • Asynchronous operations: Tasks like file I/O, network requests, or user input can be performed without blocking the main program flow.
  • Improve responsiveness: Applications, especially GUIs, remain responsive by delegating background tasks to threads.

What is multithreading in Java?

In the general context, multithreading refers to the ability of a program to execute multiple threads simultaneously, enabling concurrent execution of tasks. Multithreading enables a program to perform multiple operations in parallel within a single process.

Key Features

  • Concurrent Execution: Multiple threads can run independently and simultaneously, allowing better utilization of CPU cores.
  • Lightweight Threads: Threads in Java share the same memory space (heap) of the process, making them lightweight compared to separate processes.
  • Thread Priority: Each thread can have a priority, helping the JVM scheduler decide the order of execution.
  • Managed by JVM: The Java Virtual Machine (JVM) provides built-in support for multithreading, making it simpler to implement and manage threads.

Advantages of Multithreading:

  • Efficient CPU Utilization: Threads can utilize multiple CPU cores for parallel processing, improving application performance.
  • Responsiveness: Applications remain responsive even when performing long-running tasks (e.g., GUIs).
  • Simplified Program Design: Certain problems, like producer-consumer or parallel computation, can be solved more elegantly with threads.

Java Thread Life-cycle

The lifecycle of a thread in Java consists of several states that a thread transitions through during its execution. These states represent the various stages of a thread’s existence, from its creation to its termination.

Thread States:

  • New (Created): A thread is in the NEW state immediately after it is created, but before it is started.
  • Runnable: The thread moves to RUNNABLE state when you call the start() method on the thread. This state indicates that the thread is ready to run and is waiting for the CPU to schedule its execution. The thread is not guaranteed to run immediately. The operating system’s thread scheduler determines when it gets CPU time.
  • Running: The thread is actively executing its task (run() method). This state indicates that the thread scheduler picked the thread from the RUNNABLE pool and assigned CPU time for execution. This is the state in which the thread directly executes its operations. A thread may transition back to the RUNNABLE state if it is preempted by the scheduler or its time slice expires.
  • Blocked/Waiting/Timed Waiting: The thread is temporarily inactive and waiting for a resource or condition to be met.
    Subcategories:
    - Blocked:
    Waiting to acquire a lock or resource. This state occurs when a thread attempts to access an object that another thread has locked. Synchronization is a common scenario where blocking can happen.
    - Waiting:
    Waiting indefinitely for another thread to signal it(ex:wait(),join())
    - Timed Waiting:
    Waiting for a specific duration via sleep(milliseconds) or join(milliseconds).
  • Terminated (Dead): The thread completes the execution of all the instructions or is explicitly stopped.

Thread Lifecycle Methods:

That’s all for today!

Thank you for taking the time to read this article. I hope you have enjoyed it. If you enjoyed it and would like to stay updated on various technology topics, please consider subscribing for more insightful content.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Anji…
Anji…

Written by Anji…

Technology Enthusiast, Problem Solver, Doer, and a Passionate technology leader. Views expressed here are purely personal.

No responses yet

Write a response