Java concurrent Programming (ii) Interrupt for thread task (interrupt)

Source: Internet
Author: User

Use interrupt () to break the thread

When one thread is running, another thread can call the interrupt () method of the corresponding thread object to break it, and the method simply sets a flag in the target thread that indicates that it has been interrupted and returns immediately.

 Packagecom.csdhsm.concurrent;ImportJava.util.concurrent.TimeUnit;/*** @Title: Interruptdemo.java * @Package: Com.csdhsm.concurrent * @Description Interrupt Demo *@authorHan * @date 2016-4-18 pm 6:55:00 *@versionV1.0*/        Public classInterruptdemoImplementsRunnable {@Override Public voidrun () {System.out.println ("The thread is runing."); System.out.println ("The thread is sleeping."); Try {                        //Child thread sleeps for 20 seconds, waiting to be interruptedTimeUnit.SECONDS.sleep (20); System.out.println ("The Thread is Wake"); } Catch(interruptedexception e) {System.out.println ("The thread is interrupted"); }                //It's going to go on here .SYSTEM.OUT.PRINTLN ("The Thread is Terminal"); }         Public Static voidMain (string[] args)throwsinterruptedexception {Thread T=NewThread (NewInterruptdemo ());                T.start (); System.out.println (The Main is sleeping to wait the thread start! "); //The main thread sleeps for 2 seconds, waiting for the child thread to runTimeUnit.SECONDS.sleep (2); System.out.println ("The thread would be interrupted");    T.interrupt (); }}

Results

In particular, note the red Place: if you simply call the interrupt () method, the thread is not actually interrupted and will continue to execute.

Use the isinterrupted () method to determine the interrupt state

You can call the Isinterrupted () method on the thread object to check the interrupt state of any thread. Note here: Once the thread is interrupted, the isinterrupted () method returns True, and once the sleep () method throws an exception, it empties the interrupt flag, and the Isinterrupted () method returns FALSE.

 Packagecom.csdhsm.concurrent;ImportJava.util.concurrent.TimeUnit;/*** @Title: Interruptedcheck.java * @Package: Com.csdhsm.concurrent * @Description interrupted Test *@authorHan * @date 2016-4-18 pm 7:44:12 *@versionV1.0*/        Public classInterruptedcheck { Public Static voidMain (string[] args) {Thread T=Thread.CurrentThread (); System.out.println ("Current Thread ' s Statusa" +t.isinterrupted ()); //you interrupt yourself ~T.interrupt (); System.out.println ("Current Thread ' s STATUSC" +t.isinterrupted ()); System.out.println ("Current Thread ' s Statusb" +t.isinterrupted ()); Try{TimeUnit.MILLISECONDS.sleep (100); } Catch(interruptedexception e) {System.out.println ("Current Thread ' s STATUSD" +t.isinterrupted ()); } System.out.println ("Current Thread ' s Statuse" +t.isinterrupted ()); }}
Results

Use the interrupted () method to determine the interrupt state

  You can use the thread.interrupted () method to check the interrupt state of the current thread (and implicitly reset to false). This method returns true because it is a static method and therefore cannot be used on a particular thread, only the interrupt state of the thread that called it is reported, and if the thread is interrupted and the interrupt state is unclear. Unlike isinterrupted (), it automatically resets the interrupt state to false, the second call to the Thread.interrupted () method, and always returns false unless the thread is interrupted.

 Packagecom.csdhsm.concurrent;/*** @Title: Interruptedcheck.java * @Package: Com.csdhsm.concurrent * @Description interrupted Test *@authorHan * @date 2016-4-18 pm 7:44:12 *@versionV1.0*/        Public classInterruptedcheck { Public Static voidMain (string[] args) {Thread T=Thread.CurrentThread (); System.out.println ("Current Thread ' s Statusa" +t.interrupted ()); //you interrupt yourself ~T.interrupt (); System.out.println ("Current Thread ' s STATUSC" +t.interrupted ()); System.out.println ("Current Thread ' s Statusb" +t.interrupted ()); System.out.println ("Current Thread ' s STATUSD" +t.interrupted ()); }}

Results

Watch the red mark.

Java concurrent Programming (ii) Interrupt for thread task (interrupt)

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.