Shutdown and shutdownnow-shutdown of multi-thread tasks

Source: Internet
Author: User

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
  1. While
    (True
    ){
  2. Try
    {
  3. System. Out. println ("beat"
    ); Timeunit. milliseconds. Sleep (R. nextint (1000
    ));
  4. } Catch
    (Interruptedexception e ){
  5. // Todo auto-generated Catch Block
  6. E. printstacktrace ();
  7. }
  8. }
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
  1. While
    (! Thread. interrupted ()){
  2. Try
    {
  3. System. Out. println ("beat"
    );
  4. Timeunit. milliseconds. Sleep (R. nextint (1000
    ));
  5. } Catch
    (Interruptedexception e ){
  6. // Todo auto-generated Catch Block
  7. E. printstacktrace ();
  8. // Terminate the loop
  9. Thread. currentthread (). Interrupt ();
  10. }
  11. }
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
  1. Void
    Interruptifidle (){
  2. Final
    Reentrantlock runlock = This
    . Runlock;
  3. If
    ([B] runlock. trylock () [/B]) {
  4. Try
    {
  5. Thread. Interrupt ();
  6. } Finally
    {
  7. Runlock. Unlock ();
  8. }
  9. }
  10. }
        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
  1. Void
    Interruptnow (){
  2. Thread. Interrupt ();
  3. }

From: http://zzhonghe.javaeye.com/blog/826947

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.