Java: Difference in usage between Thread.interrupted() and Thread.isInterrupted()?

后端 未结 9 686
南旧
南旧 2020-12-12 16:28

Java question: As far as I know, there are two ways to check inside a thread whether the thread received an interrupt signal, Thread.interrupted() and Thr

相关标签:
9条回答
  • 2020-12-12 16:41

    interrupted() is static and checks the current thread. isInterrupted() is an instance method which checks the Thread object that it is called on.

    A common error is to call a static method on an instance.

    Thread myThread = ...;
    if (myThread.interrupted()) {} // WRONG! This might not be checking myThread.
    if (myThread.isInterrupted()) {} // Right!
    

    Another difference is that interrupted() also clears the status of the current thread. In other words, if you call it twice in a row and the thread is not interrupted between the two calls, the second call will return false even if the first call returned true.

    The Javadocs tell you important things like this; use them often!

    0 讨论(0)
  • 2020-12-12 16:41

    Thread interruption in Java is advisory. If you call Thread.interrupt() then it will set the flag and cancel any outstanding IO tasks (which will throw InterruptedException). However it is up to code that is executing in the thread to handle this. Doing so is called implementing the Thread interruption policy.

    However because Thread's interrupted state is shared it is important that any such handling be Thread Safe. You don't want some other thread going off and trying to do something with the interrupted flag if you are handling it. For this reason the Thread.interrupted() flag makes this atomic so it is used when you want to say: "If this thread was interrupted then I am going to deal with it). Usually this will involve cleaning up some resources. Once you are done you should probably propogate the interrupted flag so that callers can handle it. You can do this by calling Thread.interrupt again.

    0 讨论(0)
  • 2020-12-12 16:43

    The interrupted() method is a class method that always checks the current thread and clears the interruption "flag". In other words, a second call to interrupted() will return false.

    The isInterrupted() method is an instance method; it reports the status of the thread on which it is invoked. Also, it does not clear the interruption flag. If the flag is set, it will remain set after calling this method.

    0 讨论(0)
  • 2020-12-12 16:45

    If you use interrupted, what you're asking is "Have I been interrupted since the last time I asked?"

    isInterrupted tells you whether the thread you call it on is currently interrupted.

    0 讨论(0)
  • 2020-12-12 16:47

    interrupted() method is a static method of class thread checks the current thread and clear the interruption "flag".i.e. a second call to interrupted() will return false.

    isInterrupted() method is an instance method; it reports the status of the thread on which it is invoked. it does not clear the interruption flag.

    If the flag is set, it will remain set after calling this method.

    Thread myThread = ...;
    if (myThread.interrupted()) {} //error
    
    Thread.interrupted()//right
    
    if (myThread.isInterrupted()) {} // Right
    
    0 讨论(0)
  • 2020-12-12 16:49

    There are a lot of idioms surrounding InterruptedException, but the question was about checking the interrupted status explicitly.

    My understanding is that isInterrupted (the instance method) should rarely be used—mainly for logging and debugging and the like. It only gives a snapshot of the flag on a given thread, which can be outdated soon afterwards.

    The normal idiom is to check interrupted (the static method) if you are writing a task which you want to be cancelable at a certain point where it is not calling something that throws InterruptedException due to a sleep or blocking I/O call or the like. If you see the flag set, you should stop your current computation as quickly as you can, returning early or throwing an exception (perhaps InterruptedException).

    So as an example, if your task looks something like

    void process(Things[] things) throws InterruptedException {
        for (Thing thing : things) {
            thing.twiddle(); // this call throws InterruptedException
        }
    }
    

    then you do not need to do anything else; if someone calls Thread.interrupt on your thread, during the current or next twiddle call an InterruptedException will be thrown up and stop your task.

    But what if twiddle does not throw InterruptedException and generally cannot be interrupted in the middle? Say each such call takes 100ms, but things.length might be 100. Then process could be blocked for 10s even if someone is trying to interrupt it, which may be unacceptable in your application. So you can explicitly check for interrupts:

    void process(Things[] things) {
        if (Thread.interrupted()) {
            return;
        }
        for (Thing thing : things) {
            thing.twiddle();
        }
    }
    

    Here you can see why it is important that interrupted atomically checks and clears the flag: you are using it to acknowledge receipt of a message, that someone has politely requested you stop as soon as possible. (In this case, within about 100ms of the request.) You can also see why this must be a static method, operating on the current thread: it only makes sense in the context of checking whether the surrounding code should be stopped.

    Of course if the caller of process is assuming it ran to completion, simply returning as shown here would be misleading. So you might want to make process return the number of things it finished processing, or it might just be more appropriate to throw the exception up:

    void process(Things[] things) throws InterruptedException {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        for (Thing thing : things) {
            thing.twiddle();
        }
    }
    

    In this case the caller gets a (checked) exception informing them that someone else asked to stop processing in the middle. Usually the caller should just let the exception be thrown up the call stack.

    You could also reinterrupt yourself if you were unable to stop your current task yet needed to know that a request to stop it did come in, for example to cut the rest of the work short:

    void process(Things[] things) {
        boolean twiddleFully = true;
        if (twiddleFully && Thread.interrupted()) {
            twiddleFully = false;
            Thread.currentThread().interrupt();
        }
        for (Thing thing : things) {
            thing.twiddle(twiddleFully);
        }
    }
    

    Here we can process the remaining things more quickly but still complete the loop, and turn the interrupted flag back on so that our caller can decide to handle it.

    0 讨论(0)
提交回复
热议问题