Java Interrupt Note (ii)

Source: Internet
Author: User

In the previous blog post, the use of the Member method interrupt () was introduced, and the last part of the article went on to describe the remaining two methods related to interrupts.

2. Member method new Thread (). isinterrupted ()

It is common to use the Thread.CurrentThread (). isinterrupted () method to determine whether a thread has been sent an interrupt request (after one of the threads has been sent an interrupt request and must be interrupted ). Because it sets the thread interrupt flag bit to true, the interrupt flag bit is not cleared immediately, that is, the interrupt mark is not set to false. Examples of the program are:

Package com.szw.test;public class testsun {    public static  void main (String[] args) {        // test1         thread t1 = new thread () {              @Override              public void run () {                 try{                     int i=0;                     while (i++< 100000000) {                         // nothing                     }                     system.out.println ("A1");                 }catch (exception e) {                     system.out.println (" B1 ");                     system.out.println (Thread.CurrentThread (). isinterrupted ()  +  " in catch") ///Throw an exception after the interrupt flag bit is cleared                  }            };         };&Nbsp;       t1.start ();               system.out.println (t1.isinterrupted ()  +  " before");// The value used to check the interrupt flag bit         t1.interrupt ();//Do not interrupt the running thread          system.out.println (t1.isinterrupted ()  +  " after");     }}--------------Run result: false before   //before the Interrupt () method is called, the flag bit is Falsetrue after      //after calling the interrupt () method, the flag bit is true,  a1              //thread T1 calls the interrupt interrupt () method, and the standard bit is also set to true,                //but there is no interruption, because T1 is a running thread, interrupt () cannot be interrupted,                //specific in the previous "Java Interrupt little (i)" in the introduction


3. Static method Thread.interrupted ()

The static method thread.interrupted () can also be used to get the interrupt state, but the interrupt state is cleared after the interrupt state is obtained (of course, the previous value is cleared), that is, the method is called two times in a row, and the second time it must return false. Examples of programs:

Package com.szw.test;public class testsun {    public static  void main (String[] args) {        // test1         thread t1 = new thread () {              @Override              public void run () {                 try{                     int i=0;                     while (i++< 100000000) {                         // nothing                     }                     system.out.println ("A1");                     system.out.println ( thread.interrupted ()  +  " static");                     system.out.println (Thread.interrupted ()  +   " static 2");//Second Call                  }catch (exception e) {                     system.out.println ("B1");           &Nbsp;         system.out.println (Thread.currentThread (). isinterrupted ()  +  " in catch");//break flag bit after throwing an exception to clear                  }             };        };         t1.start ();              System.out.println (t1.isinterrupted ()  +  " before");         t1.interrupt ();//Do not interrupt the running thread         system.out.println ( t1.isinterrupted ()  +  " after");     }-------------------------------------- ------------running Result: False beforetrue aftera1true staticfalse static 2------------------- ------------if the T1.interrupt () is commented out, the result of running again is: False beforefalse aftera1false static             //First Call false static 2          //second call

To this about the method of interruption to the paragraph, and finally affixed to a Ali of the pen test, you can analyze the results of the operation ...

The code is as follows:

package com.szw.test;public class testthread{    public static  Void main (String[] args) {        // test1         thread t1 = new thread () {              @Override              public void run () {                 try{                     int i=0;                     while (i++<100000000) {                          // nothing                     }                     system.out.println ("A1");                 }catch (exception e) {                     system.out.println ("B1");                 }             };         };        t1.start ();         T1.interrupt ()//Do not interrupt a running thread                  //&Nbsp;test2        thread t2 = new thread () {             public void run () {                 try{                      Thread.Sleep (;               )      system.out.println ("A2");                 }catch (exception e) {                     system.out.println ("B2");                 }             };        };         t2.start ();         t2.interrupt ();                     // test3         thread t3 = new thread () {             public void run () {                 try{                     this.wait ( 50000);                     system.out.println ("A3");                 }catch (exception e) {                     system.out.println ("B3");                 }             };        };         t3.start ();         t3.interrupt ();             // test4        thread  t4 = new thread () {             public void run () {                 try{                   &Nbsp; synchronized (This) {                         this.wait (50000);                     }                      System.out.println ("A4");                 }catch (exception e) {                     system.out.println ("B4");                 }             };        };         t4.start();         t4.interrupt ();     //can be interrupted because no competition is locked                 // test5         try{             t4.start ();//exception in thread  "main"   java.lang.illegalthreadstateexception             System.out.println ("A5");         }catch (exception e) {             system.out.println ("B5");         }    }}



This article is from the "Punk" blog, please be sure to keep this source http://yimaoqian.blog.51cto.com/1328422/1545412

Java Interrupt Note (ii)

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.