Java thread------------thread death, join

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.