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

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts