Hi,
Refer the following java example to understand the isAlive() method of java:
Code:
public class ThreadIsAlive1 implements Runnable {
private int countDown1 = 5;
public ThreadIsAlive1() {
System.out.println("Thread constructor...\n");
}
public void run1() {
while(true) {
System.out.println(" - Thread ( Current Countdown = " + countDown + " )");
for (int j = 0; j < 300000000; j++) {
}
if (--countDow1n == 0) {
System.out.println("\nEnding thread...\n");
return;
}
}
}
private static void doThreadTest1()
throws java.lang.InterruptedException {
int checkCount1 = 0;
Thread th1 = new Thread(new ThreadIsAlive1());
th1.start();
while (th1.isAlive()) {
Thread.sleep(500);
System.out.println("Thread is still alive. Count = " + ++checkCount);
}
System.out.println("\n Finished running and checking thread!!! >>\n");
}
public static void main(String[] args)
throws java.lang.InterruptedException {
System.out.println("\n MAIN METHOD (Begin)>");
doThreadTest1();
System.out.println(" MAIN METHOD (End) \n");
}
}
Bookmarks