What does java.lang.Thread.interrupt() do?

前端 未结 9 2289
日久生厌
日久生厌 2020-11-22 06:01

Could you explain what java.lang.Thread.interrupt() does when invoked?

相关标签:
9条回答
  • 2020-11-22 06:28

    What is interrupt ?

    An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

    How is it implemented ?

    The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static Thread.isInterrupted, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

    Quote from Thread.interrupt() API:

    Interrupts this thread. First the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

    If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

    If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

    If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

    If none of the previous conditions hold then this thread's interrupt status will be set.

    Check this out for complete understanding about same :

    http://download.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

    0 讨论(0)
  • 2020-11-22 06:33

    For completeness, in addition to the other answers, if the thread is interrupted before it blocks on Object.wait(..) or Thread.sleep(..) etc., this is equivalent to it being interrupted immediately upon blocking on that method, as the following example shows.

    public class InterruptTest {
        public static void main(String[] args) {
    
            Thread.currentThread().interrupt();
    
            printInterrupted(1);
    
            Object o = new Object();
            try {
                synchronized (o) {
                    printInterrupted(2);
                    System.out.printf("A Time %d\n", System.currentTimeMillis());
                    o.wait(100);
                    System.out.printf("B Time %d\n", System.currentTimeMillis());
                }
            } catch (InterruptedException ie) {
                System.out.printf("WAS interrupted\n");
            }
            System.out.printf("C Time %d\n", System.currentTimeMillis());
    
            printInterrupted(3);
    
            Thread.currentThread().interrupt();
    
            printInterrupted(4);
    
            try {
                System.out.printf("D Time %d\n", System.currentTimeMillis());
                Thread.sleep(100);
                System.out.printf("E Time %d\n", System.currentTimeMillis());
            } catch (InterruptedException ie) {
                System.out.printf("WAS interrupted\n");
            }
            System.out.printf("F Time %d\n", System.currentTimeMillis());
    
            printInterrupted(5);
    
            try {
                System.out.printf("G Time %d\n", System.currentTimeMillis());
                Thread.sleep(100);
                System.out.printf("H Time %d\n", System.currentTimeMillis());
            } catch (InterruptedException ie) {
                System.out.printf("WAS interrupted\n");
            }
            System.out.printf("I Time %d\n", System.currentTimeMillis());
    
        }
        static void printInterrupted(int n) {
            System.out.printf("(%d) Am I interrupted? %s\n", n,
                    Thread.currentThread().isInterrupted() ? "Yes" : "No");
        }
    }
    

    Output:

    $ javac InterruptTest.java 
    
    $ java -classpath "." InterruptTest
    (1) Am I interrupted? Yes
    (2) Am I interrupted? Yes
    A Time 1399207408543
    WAS interrupted
    C Time 1399207408543
    (3) Am I interrupted? No
    (4) Am I interrupted? Yes
    D Time 1399207408544
    WAS interrupted
    F Time 1399207408544
    (5) Am I interrupted? No
    G Time 1399207408545
    H Time 1399207408668
    I Time 1399207408669
    

    Implication: if you loop like the following, and the interrupt occurs at the exact moment when control has left Thread.sleep(..) and is going around the loop, the exception is still going to occur. So it is perfectly safe to rely on the InterruptedException being reliably thrown after the thread has been interrupted:

    while (true) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException ie) {
            break;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:33

    An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. A very good referance: https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

    0 讨论(0)
  • 2020-11-22 06:45

    Thread.interrupt() method sets internal 'interrupt status' flag. Usually that flag is checked by Thread.interrupted() method.

    By convention, any method that exists via InterruptedException have to clear interrupt status flag.

    0 讨论(0)
  • 2020-11-22 06:46

    Thread interruption is based on flag interrupt status. For every thread default value of interrupt status is set to false. Whenever interrupt() method is called on thread, interrupt status is set to true.

    1. If interrupt status = true (interrupt() already called on thread), that particular thread cannot go to sleep. If sleep is called on that thread interrupted exception is thrown. After throwing exception again flag is set to false.
    2. If thread is already sleeping and interrupt() is called, thread will come out of sleeping state and throw interrupted Exception.
    0 讨论(0)
  • 2020-11-22 06:50

    public void interrupt()

    Interrupts this thread.

    Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

    If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

    If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

    If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

    If none of the previous conditions hold then this thread's interrupt status will be set.

    Interrupting a thread that is not alive need not have any effect.

    Throws: SecurityException - if the current thread cannot modify this thread

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