The Java code defines two classes: GetState and MyThread. The GetState class is the main class, and the MyThread class implements the Runnable interface to define the behavior of threads.
In the GetState class, the main method creates two threads (t1 and t2) by passing instances of MyThread to their constructors. Then, the start() method is called on both threads to start their execution.
The MyThread class implements the Runnable interface, and its run() method is where the actual thread behavior is defined. Inside the run() method, the code attempts to retrieve the state of the current thread using Thread.currentThread().getState(). It then prints the state of the thread to the console.
The Thread.State enum represents the possible states of a thread, which include:
Keep in mind that when you run the program, the output may vary depending on the thread scheduler and the system's behavior, as the order of execution between t1 and t2 is not guaranteed. Additionally, since the threads are short-lived and execute quickly, they might not be in certain states (e.g., BLOCKED, WAITING, or TIMED_WAITING) during the time you attempt to retrieve their states, and you might mostly see them in the RUNNABLE state.
public class GetState { public static void main(String[] args) { Thread t1 = new Thread(new MyThread()); Thread t2 = new Thread(new MyThread()); t1.start(); t2.start(); } } class MyThread implements Runnable { public void run() { try { Thread.State thrdState = Thread.currentThread().getState(); System.out.println("Thread State : " + thrdState); } catch (Exception e) { System.out.println(e); } } }
Thread State : RUNNABLE Thread State : RUNNABLE
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions