Java.lang.Thread.getState() Method



Description

The java.lang.Thread.getState() method returns the state of this thread. It is designed for use in monitoring of the system state, not for synchronization control.

Declaration

Following is the declaration for java.lang.Thread.getState() method

public Thread.State getState()

Parameters

NA

Return Value

This method returns this thread's state.

Exception

NA

Example

The following example shows the usage of java.lang.Thread.getState() method.

package com.tutorialspoint;

import java.lang.*;

public class ThreadDemo implements Runnable {

   public void run() {

      // returns the state of this thread
      Thread.State state = Thread.currentThread().getState();
      System.out.println(Thread.currentThread().getName());
      System.out.println("state = " + state);
   }

   public static void main(String args[]) {
      Thread t = new Thread(new ThreadDemo());
      
      // this will call run() function
      t.start();   
   }
} 

Let us compile and run the above program, this will produce the following result −

Thread-0
state = RUNNABLE
java_lang_thread.htm
Advertisements