java tutorial - Java thread - java programming - learn java - java basics - java for beginners
what is thread in java
- A thread in the context of Java is the path followed while executing a program.
- All Java programs have at least one thread, known as the main thread, which is created by the JVM at the program's start, when the main() method is invoked with the main thread.
Life cycle of a Thread
- A thread can have five states. According to sun, there are just 4 states in thread life cycle, new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread in java is controlled by JVM. Java thread states are given below:
- New
- Runnable
- Running
- Non-Runnable (Blocked)
- Terminated
Learn Java - Java tutorial - java thread - Java examples - Java programs
New
- The thread is in new state if we create instance of Thread class but previous to the invocation of start() method.
Runnable
- The thread is in runnable state behind invocation of start() method, but the thread scheduler has not selected it to be the running thread.
Running
- The thread is in running state if thread scheduler has selected it.
Non-Runnable (Blocked)
- The thread is in non-runnable state when the thread is still alive, but is presently not eligible to run.
Terminated
- The thread is in terminated state or dead state when its run() method exits.