Using getState in java

808 Views Asked by At

I am new to programming and I just started working with Applets on Java. I saw the command getState and understand what it's doing to the specific code but I don't know what it actually does. What is the function of getState() command on Java? What do we use it for?

1

There are 1 best solutions below

1
C. Peck On

Here's a quick example of the usage of getState() I found after searching:

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();   
    }
} 

Compiling and running gives us

Thread-0
state = RUNNABLE