Exception method Stop thread:
Public classRealinterruptthreadextendsThread {@Override Public voidrun () {Try { for(inti = 0; i < 5000000; i++) { if(thread.interrupted ()) {System.out.println ("Thread is interrupted status, I need exit."); Throw Newinterruptedexception (); } System.out.println ("I=" + (i + 1))); } } Catch(interruptedexception e) {System.out.println ("Realinterruptthread catch interruptedexception."); E.printstacktrace (); } }} Public classThreadrunmain { Public Static voidMain (string[] args) {testrealinterruptthread (); } Public Static voidTestrealinterruptthread () {Try{Realinterruptthread rit=NewRealinterruptthread (); Rit.start (); Thread.Sleep (1000); Rit.interrupt (); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("End!"); }}
Operation Result:
Java Learning Note Exception method stop thread