Why use Thread.currentThread (). IsInterrupted () with try / catch

I found this structure popular in the multi-threaded chapter Thinking in Java:

public void run(){
    try{
        while(!Thread.currentThread().isInterrupted()){
            // do something blocked like wait(), sleep() or I/O
        }
    }catch(InterruptedExeption e){

    }
}

      

However, now I think the while loop will terminate if and only if blocked wait()

, sleep()

or I/O

throws InterruptedExeption

, why not use while(true)

instead? Or simply because it is Thread.currentThread().isInterrupted()

just canonical?

+3


source to share


4 answers


The interrupt check itself is unnecessary, since all Runnables execute the sleep function. But, as you can tell from the comment, this is intended as a model for larger procedures. While sleep and wait throw InterruptedException will block I / O calls (listed in the comment), there won't, and CPU-intensive code that doesn't wait and won't sleep won't.



What Bruce E does here is you are provided with a template for you that handles as a case when an InterruptedException is thrown (where it uses it to break out of the loop) and also handles cases where no sleep occurs or an InterruptedException is thrown. This allows the reader to understand that they have the ability to explicitly check the flag and demonstrates the correct way to do this (as opposed to using an interrupted method that clears the flag).

+3


source


You can also interrupt the Thread with Thread.interrupt()

external help . So to speak, it's basically a built-in boolean flag.



In this case, you are right. You can use while(true)

since it will exit the loop when it happens InterruptedException

. The author probably wanted to explain this in a related question. There is probably a chapter on how to stop an endless stream.

0


source


This is a contrived and rather bad example. You're right, which while (true)

is a great substitute.

sleep

and are wait

already checking the same flag. They use this to know if an exception needs to be thrown.

Therefore this code:

public void run(){
    try{
        while(!Thread.currentThread().isInterrupted()){
            Thread.sleep(100);
        }
    }catch(InterruptedExeption e){

    }
}

      

basically presents logic like this (pseudocode):

try {
    while(!Thread.currentThread().isInterrupted())
    {
        /* Thread.sleep */
        boolean keepSleeping = true;
        while(keepSleeping)
        {
            if (Thread.currentThread().isInterrupted())
            {
                throw new InterruptedExeption();
            }
            actualSleep(/*a very small amount of time*/);
            if (/* we've slept long enough*/)
            {
                keepSleeping = false;
            }
        }
        /* End Thread.sleep */
    }
}catch(InterruptedExeption e){

}

      

which is clearly tortuous and pointless.

What else Thread.sleep

is actually calling interrupted

, and not isInterrupted

, which is slightly different in that it actually resets the flag back, so the flow appears to be continuous.

0


source


Apparently according to API Doc :

If this thread is blocked when calling the methods wait (), wait (long), or wait (long, int) of the Object class or join (), join (long), join (long, int), sleep (long), or sleep (long, int), methods of this class, then its interrupted state will be cleared and it will receive InterruptedException .

Thus, the return value isInterrupted()

and InterruptedException

does not necessarily mean the same thing. See the API doc for other terms.

0


source







All Articles