1 Packagecom.imooc.base;2 3 Public classWrongwaystopthreadextendsThread {4 5 Public Static voidMain (string[] args) {6Wrongwaystopthread thread =NewWrongwaystopthread ();7SYSTEM.OUT.PRINTLN ("Starting thread ...");8 Thread.Start ();9 Try {TenThread.Sleep (3000); One}Catch(interruptedexception e) { A e.printstacktrace (); - } -SYSTEM.OUT.PRINTLN ("Interrupting thread ..."); the Thread.Interrupt (); -SYSTEM.OUT.PRINTLN ("Thread interrupted?") +thread.isinterrupted ()); - - Try { +Thread.Sleep (3000); -}Catch(interruptedexception e) { + e.printstacktrace (); A } atSYSTEM.OUT.PRINTLN ("Stopping application ..."); - } - Public voidrun () { - while(! This. isinterrupted ()) { -System.out.println ("Thread is running ..."); - //Long time = System.currenttimemillis (); in //While (System.currenttimemillis ()-time <) { - // to // } + Try { -Thread.Sleep (3000); the}Catch(interruptedexception e) { * e.printstacktrace (); $ }Panax Notoginseng BooleanFlag =thread.interrupted (); - System.out.println (flag); the } + } A}
Java Multithreading 3