The difference between interrupt(), interrupted() and isInterrupted() in the Thread class

Regarding the three methods in the Java Thread class, they look similar, but only interrupt interrupts the thread, and interrupted and isInterrupted return boolean values.

1. interrupt: When the corresponding thread calls this method, it will mark the thread as interrupted.

    public void interrupt() {
        if (this != Thread.currentThread())
            checkAccess();

        synchronized (blockerLock) {
            Interruptible b = blocker;
            if (b != null) {
                interrupt0();           // Just to set the interrupt flag
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }

2. interrupted: Determine whether the currently executing thread is interrupted, and clear the interrupt flag. The current execution here refers to the thread when this thread is running, not the thread that calls this method. For example, there are thread A and thread B, even if the a.interrupted() method is called in thread B, the return is to determine whether thread B is interrupted.

    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

3. isInterrupted: Determine whether the thread calling this method is interrupted, and the interrupt flag is not cleared. But if the interrupted thread continues to execute, calling it again returns false.

    public boolean isInterrupted() {
        return isInterrupted(false);
    }

Sample code:

public class Test {
    public static void main(String[] args) throws  Exception{
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    System.out.println(1111); // 3
                    if(Thread.currentThread().isInterrupted()){
                        System.out.println("t1 interrupt");
                        System.out.println(Thread.currentThread().isInterrupted());
                        break;
                    }
                }
            }
        });
        t1.start();
        //此处睡一下是为了让t1线程启动
        Thread.sleep(200);
        t1.interrupt();
        System.out.println(t1.isInterrupted());// 1
        System.out.println(t1.isInterrupted());// 2
        System.out.println(t1.isInterrupted());// 4
        System.out.println(t1.isInterrupted());// 5
        System.out.println("---------------------");
        Thread.currentThread().interrupt();
        System.out.println(Thread.interrupted()); // 7
        System.out.println(Thread.interrupted()); // 8
    }
}

Notes marked with numbers in the figure need break points, and are executed in the order of the numbers.

Run the main thread, because t1.interrupt is executed, so breakpoint 1 prints true.

Further down, breakpoint 2 is also true. Explain that the isInterrupted method does not reset the flag.

At this time, we switch to the t1 thread, execute one line, and breakpoint 3.

Then switch back to the main thread and continue to execute breakpoint 4.

It can be seen that as long as the interrupted thread executes a line, when we call again to determine whether it is interrupted, it will return false. Continuing to execute breakpoint 5 is still false.

We continue to switch to the t1 thread

It can be seen that although the main thread judges whether it is interrupted or not, it has already displayed false, but it will still be true when executed in its own thread, enter the method and print the statement.

Go back to the main thread and execute the Thread.currentThread().interrupt() method to interrupt the current main thread. I take the code down

        System.out.println("---------------------");
        Thread.currentThread().interrupt();
        System.out.println(Thread.interrupted()); // 7
        System.out.println(Thread.interrupted()); // 8

At this time, our breakpoint 7 executes the interrupted method.

The interrupted method will interrupt the mark after the first call. So breakpoint 7 prints that the current main thread is interrupted as true, and breakpoint 8 as false.

No matter which thread calls this method, it only judges whether the executing thread is interrupted. We can change the code to t1.interrupted.

It can be seen that the result is the same.

Guess you like

Origin blog.csdn.net/yytree123/article/details/108896876