How to get Java Thread State

Release 5.0 introduced the Thread.getState() method. When called on a thread, one of the following Thread.State values is returned:
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED

Example

package com.pretech;
public class ThreadState {
	public static void main(String[] args) {
		Threadone t1 = new Threadone();
		System.out.println(t1.getState());
		t1.start();
		System.out.println(t1.getState());
	}
}
class Threadone extends Thread {
	public void run() {
		System.out.println("HelloWorld");
	}
}

Output



NEW
RUNNABLE
HelloWorld
TERMINATED


No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts