Java.lang.InterruptedException:sleep interrupted

Source: Internet
Author: User

Java High concurrency

Problem Description: Interrupt of current if judgment ' if in sleep, even if outside interruption, inside will also orderly exit,

But when the if judge is behind the sleep, he will want to interrupt the exception (not enough time to cause main is also a thread)

Package com.example.echo.Lock; /** * When Thread writes where, when the current process affects//** */class Interrupted_root_make extends thread{@Override public void R


            Un () {while (true) {System.out.println ("-----"); /** * Use the interrupt command, the outside is only a notification command, the actual interruption of the operation or the need to judge the internal operation, exit/try {Thread.s
            Leep (1000);
            catch (Interruptedexception e) {e.printstacktrace ();
                } if (Thread.CurrentThread (). isinterrupted ()) {System.out.println ("I has interputed");
            Break
        } Thread.yield ();
        }} public class Interruped_test {public static void main (string[] args) throws Interruptedexception {
        Interrupted_root_make interrupted_root_make = new Interrupted_root_make ();

        Interrupted_root_make.start ();




    Interrupted_root_make.interrupt (); }
}

Right to

Class Interrupted_root_make extends thread{

    @Override public
    void Run () {while
        (true) {

            if ( Thread.CurrentThread (). isinterrupted ()) {
                System.out.println ("I has interputed");
                break;
            SYSTEM.OUT.PRINTLN ("-----");


            /**
             * Use the interrupt command, the outside is only a notification command, the actual interruption of the operation or the need to internal judgment operation, exit/
            try {
                thread.sleep;
            } catch ( Interruptedexception e) {
                e.printstacktrace ();
            }



            Thread.yield ();
}} public class Interruped_test {public
    static void Main (string[] args) throws Interruptedexception {
        Interrupted_root_make interrupted_root_make = new Interrupted_root_make ();
        Interrupted_root_make.start ();
        Thread.Sleep (1000);
        Interrupted_root_make.interrupt ();




    }


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.