The Java threading meeting ends in the following three ways, and it is in a state of death after the end
1. run() or call() method is executed and the thread ends normally;
2. The thread throws an uncaught Exception or Error;
3. Directly call the stop() method of the thread to end the thread;
Note: When the main thread ends, other threads are not affected in any way and will not end with it. Once the child thread is started, it has the same status as the main thread and will not be affected by the end of the main thread.
In order to test whether a thread has died, the isAlive() method of the thread object can be called. When the thread is in the three states of ready, running, and blocking, the method will return true; when the thread is in the new state and dead state, the method will return false.
The following tests are performed on 1 and 2 of thread death.
The main thread's code 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: After the thread ends normally, isAlive() returns False
Write thread execution code where thread ends normally:
public class RunTask implements Runnable { @Override public void run() { for (int idx = 1; idx <= 10; idx++) { System.out.println("Subthread: I'm still alive" + idx); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }} The output results of the two threads are as follows. After the normal execution of the child thread is completed, using Thread.isAlive() will return False.
Main thread: child thread state is true Main thread: child thread state is true Sub-thread: I am still alive 8 Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Sub-thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false
Test 2: After the child thread throws an exception, the thread's isAlive() returns False
Modify the code of the child thread and add exception throws:
public class RunTask implements Runnable { @Override public void run() { for (int idx = 1; idx <= 10; idx++) { System.out.println("Subthread: 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 state is true Main thread: child thread state is true Main thread: child thread state is true Sub-thread: child thread state is true Sub-thread: child thread state is true Main thread: child thread state is true Sub-thread: child thread state is true Sub-thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true Main thread: child thread state is true java.lang.RuntimeException: i am die at RunTask.run(RunTask.java:15) at java.lang.Thread.run(Thread.java:662) Main thread: child thread state is false Main thread: child thread state is false Main thread: child thread state is false
It can be seen that after the exception is thrown, the child thread terminates directly and becomes the Flase state;
Summarize
After the thread ends normally, or the thread throws an uncaught exception, the thread becomes dead, and the isAlive() function returns False. Okay, the above is the entire content of this article. I hope the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.