An analysis of several instances of thread death in Java

  • 2020-05-27 05:41:16
  • OfStack

The Java thread meeting ends in the following three ways, and ends in a dead state

1. run() or call() Method execution completes, thread ends normally;

2. Thread throws 1 uncaptured Exception or Error;

3. Call the thread directly stop() Method to terminate the thread;

Note: when the main thread ends, the other threads are not affected and do not end with it. Once a subthread is started, it has the same status as the main thread and is not affected by the end of the main thread.

To test whether a thread is dead, call the isAlive() Method, which returns true when a thread is ready, running, or blocked. This method returns false when the thread is in either new or dead state.

Test 1 and 2 for thread death below.

The code of the main thread is as follows:


public class ThreadTest {
 public static void main(String[] args) throws InterruptedException {
  Thread t = new Thread(new RunTask());
  t.start();
  
  while (true) {
   Thread.sleep(1000);
   System.out.println(" Main thread: child thread status is " + t.isAlive());
  }
 }
}

Test 1: when the thread ends normally, isAlive() returns False

Write thread execution code that ends normally:


public class RunTask implements Runnable {
 
 @Override
 public void run() {
  for (int idx = 1; idx <= 10; idx++) {
   System.out.println(" Child thread: I'm still alive " + idx);
   
   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}

The output of the two threads is shown below, showing that the child thread has finished its normal execution and is used Thread.isAlive() I'm going back to False.


 Main thread: child thread status is true
 Main thread: child thread status is true
 Child thread: I'm still alive 8
 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is true
 Child thread: I'm still alive 9
 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is true
 Child thread: I'm still alive 10
 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is false
 Main thread: child thread status is false
 Main thread: child thread status is false
 Main thread: child thread status is false
 Main thread: child thread status is false

Test 2: after the child thread throws an exception, the thread's isAlive() returns False

Modify the code of the child thread to add exception throwing:


public class RunTask implements Runnable {
 
 @Override
 public void run() {
  for (int idx = 1; idx <= 10; idx++) {
   System.out.println(" Child thread: I'm still alive " + idx);
   
   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   
   if (idx == 5) {
    throw new RuntimeException("i am die");
   }
  }
 }
}

Execute again and observe the output:


 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is true
 Child thread: I'm still alive 4
 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is true
 Child thread: I'm still alive 5
 Main thread: child thread status is true
 Main thread: child thread status is true
 Main thread: child thread status is true
Exception in thread "Thread-0" java.lang.RuntimeException: i am die
 at RunTask.run(RunTask.java:15)
 at java.lang.Thread.run(Thread.java:662)
 Main thread: child thread status is false
 Main thread: child thread status is false
 Main thread: child thread status is false

As you can see, after the exception is thrown, the child thread terminates directly and becomes the Flase state.

conclusion

After the thread ends normally or the thread throws an uncaught exception, the thread becomes dead and isAlive() function is used to return False. Well, the above is the entire content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: