1 while(true){2 Try { 3Sleep (1000);4}Catch(interruptedexception e) {5 //TODO auto-generated Catch block6 e.printstacktrace ();7 }8}
Like the code above, the thread does not stop using the Executorservice object to call the Shutdownnow method or the Shutdown method. To stop the thread, you need to change to the following code:
1 while(!thread.interrupted ()) {2 3 4 5 Try {6Sleep (1000);7}Catch(interruptedexception e) {8 e.printstacktrace ();9 //End LoopTen Thread.CurrentThread (). interrupt (); One}
The difference between the Shutdownnow method and the Shutdown method:
1, Shutdown method: This method, can only immediately interrupt those who currently have no task, in the waiting state from Blockingqueue to get the task exception. You cannot interrupt the thread that is in the execution of the task, or the thread that is suspended during the execution of the task.
2, Shutdownnow method: Regardless of whether the task is in the execution, all interrupt, not to judge what locks do not lock.
Stop running a thread in Executorservice