What code can catch an interruptedException?

I read about it in practice. Now I want to understand how to handleInterrruptedException

Tips from the book:

- throw an exception (perhaps after some cleaning of the task) which makes your method an intermittent method of blocking; or
- restore the interrupt status so that the code above in the call stack can handle this.
- only that implements a thread interrupt policy can internalize an interrupt request. Task and general purpose library code should never swallow interrupt requests.

The first two statements are clear to me, but I do not understand the third. Can you clarify this? Preferred example.

update (thanks to Shubham for the link)

Once it’s acceptable to learn an interrupt when you know the thread is about to exit. This scenario only occurs when the interrupt method call class is part of a stream, not Runnable or general-purpose library code , as shown in Listing 5. This creates a stream that lists primes until it is interrupted and allows the stream break down. The prime minister loop checks for interruption in two places: once by polling isInterrupted () in the header of the while loop and once when he calls the blocking method BlockingQueue.put ().

public class PrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;

    PrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!Thread.currentThread().isInterrupted())
                queue.put(p = p.nextProbablePrime());
        } catch (InterruptedException consumed) {
            /* Allow thread to exit */
        }
    }

    public void cancel() { interrupt(); }
}

Now I do not understand the bold text.

+4
source share
3 answers

ExecutorService will be an example of a third operator.

runnables (/callables) "" .

runnable , runnables.

, ExecutorService , runnable, runnable.

+1

, , - , . InterruptedException, ( try), , InterruptedException interrupt, true, interrupt.

, , InterruptedException []. InterruptedException , .

, - , . , .

0

: , .

, , , . , , , , "", .

. .

0

Source: https://habr.com/ru/post/1669666/


All Articles