Describe ways to move a thread from one state to another.
Threads run as part of the system and perform tasks simultaneously and it is difficult to know how they behave. In addition, Java threads have definite life cycles.
Let us start by looking at the lifecycle of an application thread. Every operating system has a view of threads that can differ in the details (but in most cases is broadly similar at a high level).
Java tries hard to abstract these details away, and has an enum called Thread.State.
The values of Thread.State provide an overview of the lifecycle of a thread:
Thread life cycles
NEW: The thread has been created but its start() method has not yet been called. All threads start in this state.
RUNNABLE: The thread is running or is available to run when the operating system schedules it.
BLOCKED: The thread is not running because it is waiting to acquire a lock so that it can enter a synchronized method or block. We will see more about synchronized methods and blocks later in this section.
WAITING: The thread is not running because it has called Object.wait() or Thread.join().
TIMED_WAITING - (Sleeping, Waiting): The thread is not running because it has called Thread.sleep() or has called Object.wait() or Thread.join() with a timeout value.
TERMINATED: The thread has completed execution. Its run() method has exited normally or by throwing an exception.