The interrupt for the thread. Interrupt

Source: Internet
Author: User
Tags throw exception

The thread object. Interrupt ()

Note that there is a break in the exception analysis, otherwise it cannot be interrupted

 Public classDemoextendsJFrame {PrivateThread thread;//Defining Threads    FinalJProgressBar ProgressBar =NewJProgressBar ();//progress bar     PublicDemo () {setbounds (100, 100, 200, 100);        Setdefaultcloseoperation (Windowconstants.exit_on_close);        Getcontentpane (). Add (ProgressBar, Borderlayout.north); Progressbar.setstringpainted (true);//Show Numbers//implementing thread objects using anonymous inner classesThread =NewThread () {intCount = 0;//Progress Data             Public voidrun () { while(true) {//infinite loops, typically used when the number of cycles is not knownProgressbar.setvalue (+ +)count); if(Count = = 20) {                        //Thread.Interrupt ();                         This. Interrupt (); }                    Try{Thread.Sleep (100);//Sleep 0.1s}Catch(interruptedexception e) {e.printstacktrace (); System.out.println ("20% were interrupted");//Throw Exception                         Break;//there must be a break, otherwise it cannot be interrupted                    }                }            }        };        Thread.Start (); SetVisible (true); }     Public Static voidMain (string[] args) {NewDemo (); }}

The interrupt for the thread. 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.