I am doing some testing to understand the different thread states in a java, and got struck with a few queries.
Normally, when a thread is instantiated it is said to be in the "NEW" state and then when invoked the start() method on it, the operating system scheduler gets the control and is in "RUNNABLE" state and further when run() is invoked internally by start() it is said to be in running state.
Thread.currentThread().getState() // Returns the state of the thread
But observed when executed the below code, the thread state never gets displayed as "RUNNING" even when tested inside the run method.
Can anyone help me understand why does it behave like this ?
public static void main(String[] args) {
System.out.println("Hello World");
Thread t=new Thread(()->{
System.out.println("Hi");
System.out.println(Thread.currentThread().getState()); //// STATE DISPLAYED AS "RUNNABLE" AGAIN
});
System.out.println(t.getState()); // STATE DISPLAYED AS "NEW"
t.start();
System.out.println(t.getState()); // STATE DISPLAYED AS "RUNNABLE"
}
lambda expression is used to implement the run() method where we are testing the state of the thread which again displays as "RUNNABLE" instead of "RUNNING"