14.01. Multithreading Overview

Threads Overview

  • Eclipse: Oxygen
  • Java: 1.8

Threads perform a single task that belongs to a single process. The overview of threads is provided here along with the states of the threads. It is controlled by java.lang.Thread class.

A thread is a mini process within a process that can execute or perform operations independently. A thread is a lightweight process. Unlike many other computer languages, Java provides integrated support for multi-threaded programming. A multi-process program contains two or more parts that can be executed simultaneously. Every Java program contains atleast one thread known as the main thread.

Multithreading

A process of executing multiple threads simultaneously is known as Multithreading. However, we use multithreading rather than multiprocessing because threads use a shared memory area. Hence, this saves memory and therefore threads don’t have to allocate separate memory area.

Each part of that program is called a subprocess and each subprocess defines a different execution path. Therefore, multithreading is a specialized form of multitasking.

In a single-threaded application, only one thread is executed at a time because the application or the program can only handle one task at a time.

The following snippet demonstrates that threads exist in several states:

  • New: when we create an instance of the Thread class, a thread is in a new state.
  • Runnable State: Runnable status is a state where the thread is ready to run.
  • Running state: A thread is executing where multi-threaded programming is derived by the hardware.
  • Blocked/ waiting state: A Java thread can be blocked when a resource is expected.
  • Terminated/ dead state: A thread can be terminated, which stops its execution immediately at any time. Once a thread is finished, it cannot be resumed.

Contributed by Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments