- 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.
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.
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