The thread pool of 5.0 is used to close the thread. In any case, the interrupt. And interrupt method can be called to end the thread and release resources in no case. Interrupt only throws an exception when the thread is blocked to end the blocking.
For example, no matter how shutdown or shutdownnow the code below, it will not be closed:
Java code
- While
(True
){
- Try
{
- System. Out. println ("beat"
); Timeunit. milliseconds. Sleep (R. nextint (1000
));
- } Catch
(Interruptedexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
while(true){try {System.out.println("beat");TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
Therefore, when we use a thread pool, we not only need to know how to enable it, but also how to close it. The following is a good method:
Java code
- While
(! Thread. interrupted ()){
- Try
{
- System. Out. println ("beat"
);
- Timeunit. milliseconds. Sleep (R. nextint (1000
));
- } Catch
(Interruptedexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- // Terminate the loop
- Thread. currentthread (). Interrupt ();
- }
- }
While (! Thread. interrupted () {try {system. out. println ("Beat"); timeunit. milliseconds. sleep (R. nextint (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace (); // end the loop thread. currentthread (). interrupt ();}}
Finally, executorservice. Shutdown (); or showdownnow () must be called to terminate the task.
Note the following points:
1. thread. interrupted () is a method to test whether the thread has been called interrupt, because if
Java code
If it is called, the thread will throw an exception very rudely next time it is wait. interrupted () returns a Boolean to indicate whether this situation exists. In addition, it also clears an called interrupt. (Only one can be cleared)
2. Shutdown method:
This method can only interrupt the exceptions that currently do not have a task and are in the waiting state to get the task from blockingqueue. But cannot interrupt those in the task
The thread in the execution process, or the thread suspended during the execution of the task. Check the implementation code to find out the cause:
Java code
- Void
Interruptifidle (){
- Final
Reentrantlock runlock = This
. Runlock;
- If
([B] runlock. trylock () [/B]) {
- Try
{
- Thread. Interrupt ();
- } Finally
{
- Runlock. Unlock ();
- }
- }
- }
void interruptIfIdle() { final ReentrantLock runLock = this.runLock; if ([b]runLock.tryLock()[/b]) { try { thread.interrupt(); } finally { runLock.unlock(); } } }
The worker has a lock during execution. If the task is not completed, the lock will not be released.
3. shutdownnow method: no matter whether the task is being executed or not, interrupt is used to determine which locks are not locked.
Java code
- Void
Interruptnow (){
- Thread. Interrupt ();
- }
From: http://zzhonghe.javaeye.com/blog/826947