Java thread------------thread death, join
package java_thread;/* * thread death status * causes thread death * 1, run () or call () when the following 3 cases occur The method executes, the thread ends normally * 2, the thread throws an uncaught exception or error * 3, and the thread's Stop () method is called directly to end the thread (note that the method is outdated, possibly or causes a deadlock) */public class startdead extends thread {private int i = 0 ;/* * thread Body */@Overridepublic void run () {for (; i < 100; i++) {system.out.println (GetName () + " " + i);}} Public static void main (String[] args) {StartDead sd = new Startdead ();for (int i = 0; i < 300; i++) {System.out.println ( Thread.CurrentThread (). GetName () + " " + i);if (i == 20) { Sd.start (); System.out.println (Sd.isalive ());} if (I > 20 && !sd.isalive ()) {//must be the output falseSystem.out.println ( Sd.isalive ());* * a Exception */// sd.start () is reported when attempting to start a thread that is already dead;}}} The join () method of the package java_thread;/* * thread * 1, Join () Method: A method that allows one thread to wait for another thread to complete * 2, When another thread's join () method is called in a thread execution stream, the execution stream (such as main) is blocked and must wait until the other thread performs the play */public class jointhread extends Thread{public static JoinThread jt = null;/* * provides a parameterized constructor to set the name */public jointhread (string name) {super (name);} /* * rewrite the Run () method, define thread body */@Overridepublic void run () {for (int i = 0; i < 100; i++) {system.out.println (GetName () + " " + i);}} Public static void main (String[] args) throws Exception{/* * start child thread */jointhread a = new jointhread ("New Thread"); A.start ();//a.join (); Here too, the following program must wait for "new thread" Execution is performed to execute// main:1~19 with "new thread", the thread being join is randomly executed for (int i = 0; i < 100; i++) {if (i==20) {jt = new jointhread ("Thread of Join"); Jt.start ();/* * The main thread calls the join () method of the JT thread * main the thread must wait for the JT execution to finish before it executes *///jt.join ();} /* * so main:20~99 must be the last executed */system.out.println (Thread.CurrentThread (). GetName () + " "+i);}}}
This article is from the "@coder" blog, be sure to keep this source http://smallcoder.blog.51cto.com/11941149/1859992
Java thread------------thread death, join