Java thread stop method interrupt method Parsing

Source: Internet
Author: User

Parse the interrupt () method. For laruence, it's just a joke that a newbie has encountered such a problem,

It is estimated that when you went to school, the teacher did not say well, and there was no specific explanation at all. This is what my java teacher did, Khan! ------

For details, seeCodeAnnotations .......

 /**  * Method for stopping a thread *  @ Author  AA *  */  Public   Class  Threadinterrupt {  Private   Volatile  Static   Boolean Stop = False  ;  /**  *  @ Param  ARGs *  @ Throws  Interruptedexception  */      Public   Static   Void Main (string [] ARGs) Throws Interruptedexception {  //  Thread startup Thread01 ti1 = New  Thread01 (); ti1.start ();  //  The main thread sleeps for 1 second. Thread. Sleep (1000 );  /** *ProgramRun here. The TI thread has been started and the Ti thread is blocked. * The stop method is blocked. * The interrupt () method is called to exit the blocking state, the thread that calls this method will throw an interrupt exception, * interruptedexception. After the blocking state is terminated, next, the thread continues to run. * as shown below, "output after thread blocking ends" is printed to the console. ** conclusion: interrupt () is the blocking state used to exit the thread, if the thread is not in the blocking status, * calling this method does not play any role. The Chinese JDK says "thread interruption", which is literally misleading. (Probably Machine Translation) * Wait (), join (), and sleep () All block threads.  */  Ti1.interrupt (); system. Out. println ();  //  Thread startup          /** * The correct way to stop a thread is to set the shared variable. In any case, the exit of the thread is to check the shared variable and then stop it. ** exceptions are not considered. * after the ti2 thread starts, * After the mian thread sleep for 2.5 seconds, Set stop to true. * at this time, the ti2 thread is in the blocking status. * after the stop is set, the interrupt () method is executed to terminate the blocking, * The thread continues to run. When the stop = true occurs, it jumps out of the while and the thread stops.  */  Thread02 ti2 = New  Thread02 (); ti2.start (); thread. Sleep ( 2500 ); Stop = True  ; Ti2.interrupt (); thread. Sleep ( 1000 );} Private   Static   Class Thread01 Extends  Thread {@ override  Public   Void  Run () {system. Out. println ( "Thread01 thread startup" );  Try  {Sleep ( 1000000 ); //  Wait 1000 seconds } Catch (Interruptedexception e) {system. Out. println ( "Thread01 thread blocking stops" );} System. Out. println ( "Thread01 output after thread blocking ends" );}}  Private   Static   Class Thread02 Extends  Thread {@ override  Public   Void  Run () {system. Out. println ( "Thread02 thread startup" ); Int I = 0 ;  While (! Stop) {system. Out. println (I ++ );  Try  {Thread. Sleep ( 1000 );}  Catch  (Interruptedexception e) {system. Out. println ( "Thread02 thread blocking stops" );}}}}} 

 

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.