Is thread alive : Thread Status « Threads « Java






Is thread alive

Is thread alive
 

public class TwoThreadAlive extends Thread {
  public void run() {
    for (int i = 0; i < 10; i++) {
      printMsg();
    }
  }

  public void printMsg() {
    Thread t = Thread.currentThread();
    String name = t.getName();
    System.out.println("name=" + name);
  }

  public static void main(String[] args) {
    TwoThreadAlive tt = new TwoThreadAlive();
    tt.setName("Thread");

    System.out.println("before start(), tt.isAlive()=" + tt.isAlive());
    tt.start();
    System.out.println("just after start(), tt.isAlive()=" + tt.isAlive());

    for (int i = 0; i < 10; i++) {
      tt.printMsg();
    }

    System.out.println("The end of main(), tt.isAlive()=" + tt.isAlive());
  }
}
           
         
  








Related examples in the same category

1.Thread sleepThread sleep
2.Another way to stop a threadAnother way to stop a thread
3.Another way to suspend and resumeAnother way to suspend and resume
4.Visual suspend and resumeVisual suspend and resume
5.Thread sleep and interruptThread sleep and interrupt
6.Daemon ThreadDaemon Thread
7.Pausing the Current Thread: a thread can temporarily stop execution.
8.Pausing a Thread: set a variable that the thread checks occasionally, call Object.wait()
9.set Uncaught Exception Handler
10.Monitor a thread's status.
11.Pause the execution
12.Interrupt a thread.
13.Stopping a Thread: set a variable that the thread checks occasionally
14.Determining When a Thread Has Finished
15.Add a delay
16.Pause the execution of a thread using sleep()