The Java code defines two classes: CheckThread and MyThread. The CheckThread class is the main class, and the MyThread class implements the Runnable interface to define the behavior of the thread.
In the CheckThread class, the main method creates a new thread t by passing an instance of MyThread to its constructor. The code then prints the status of the thread using the isAlive() method before and after starting the thread.
The isAlive() method is used to check if a thread is still alive or has terminated. It returns true if the thread has been started and has not yet completed (i.e., is still running) and false otherwise. Here's what happens step by step when you run this code:
public class CheckThread { public static void main(String[] args) { Thread t = new Thread(new MyThread()); System.out.println("Thread isAlive : " + t.isAlive()); t.start(); System.out.println("Thread isAlive : " + t.isAlive()); } } class MyThread implements Runnable { public void run() { try { System.out.println("Thread is running."); } catch (Exception e) { System.out.println(e); } } }
Thread isAlive : false Thread isAlive : true Thread is running.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions