Detailed usage of Thread.currentThread().interrupt()

Basic usage

Thread.currentThread().interrupt()` is a method used in Java to interrupt the current thread. It has the following features and usage:

  1. Calling Thread.currentThread().interrupt()will set the current thread's interrupt status to "interrupted".
  2. When a thread is interrupted, its interruption status is set to true.
  3. If the thread is in a blocked state (such as calling methods such as sleep(), wait(), join()etc.), then calling interrupt()the method will throw InterruptedExceptionan exception and clear the interrupt status.
  4. In other cases, calling interrupt()the method will only set the interrupt status, and you need to determine whether to exit by judging the interrupt status of the thread.

Here are Thread.currentThread().interrupt()some common uses and considerations for :

  • Respond to interrupts: When a thread needs to respond to an external interrupt request, it can call the method at the appropriate location interrupt()to check the interrupt status and handle it accordingly.
  • Exit the loop/task: During the execution of the loop or task, you can call the method under appropriate conditions interrupt()to interrupt the thread, and determine the interruption status in the loop to safely exit the loop or task.
  • Passing an interrupt: If a thread has a child thread, and the child thread also needs to respond to an interrupt request, you can call the method on the child thread in the parent thread interrupt()to pass the interrupt request.
  • Restore interrupt status: When catching InterruptedExceptionan exception, you should usually re-interrupt the current thread so that the interrupt status can be passed to the upper caller.

It should be noted that after calling interrupt()the method, the thread will not stop or exit immediately, but will decide whether to continue execution by checking the interrupt status. Therefore, developers need to check the interrupt status of the thread at the appropriate time and handle it accordingly, such as stopping the loop, releasing resources, etc.

Summary: Thread.currentThread().interrupt()It is a method used to set the interrupt status of the current thread to "interrupt", which can help the thread respond to interrupt requests and handle them accordingly.

Combining examples

Thread.currentThread().interrupt()Used to interrupt the current thread and set its interrupt status to "interrupted". It can be used in a variety of scenarios. The following are detailed usage and precautions about this method:

  1. InterruptedExceptionRe-interrupt the thread when the exception is caught :

    try {
          
          
        // 可能会抛出 InterruptedException 的代码块
    } catch (InterruptedException e) {
          
          
        Thread.currentThread().interrupt(); // 重新中断线程
        // 处理中断异常
    }
    

    When an exception is caught InterruptedException, the current thread should usually be reinterrupted to pass the interrupt status to the upper caller.

  2. Interrupt the current thread and exit the loop:

    while (!Thread.currentThread().isInterrupted()) {
          
          
        // 循环体代码
    
        if (需要中断条件) {
          
          
            Thread.currentThread().interrupt(); // 中断当前线程
        }
    }
    

    When the conditions that need to exit the loop are met, interrupt()the current thread is interrupted by calling the method. Then, at the beginning or end of the loop, check the thread's interrupt status and exit the loop if interrupted.

  3. Passing and processing interrupt requests between threads:
    interrupting the child thread in the parent thread:

    Thread childThread = new Thread(() -> {
          
          
        while (!Thread.currentThread().isInterrupted()) {
          
          
            // 子线程的任务代码
        }
    });
    
    childThread.start();
    // ...
    childThread.interrupt(); // 中断子线程
    

    By calling the method on the child thread in the parent thread interrupt(), you can pass an interrupt request to the child thread to exit the loop or task.

  4. Respond to external interrupt requests:
    Check the interrupt status at the appropriate time and handle it accordingly:

    while (循环条件) {
          
          
        if (Thread.currentThread().isInterrupted()) {
          
          
            // 中断状态为 true,进行相应的处理
            break; // 或者其他适当的操作
        }
        // 循环体代码
    }
    

    During loop or task execution, use isInterrupted()the method to check the thread's interrupt status and handle it if necessary.

It should be noted that Thread.currentThread().interrupt()the method will only set the interrupt status and will not immediately stop the execution of the thread. Developers need to check the interrupt status of the thread at the appropriate time and handle it accordingly, such as stopping the loop, releasing resources, etc.

Summary: Thread.currentThread().interrupt()It is used to interrupt the current thread and set its interrupt status to "interrupt". It can be used in scenarios such as re-interrupting threads, exiting loops, passing interrupt requests, and responding to external interrupt requests. The interrupt status of the thread should be checked at an appropriate time according to the specific situation, and related processing should be carried out.

If you do not use Thread.currentThread().interrupt()the method, that is, do not call this method to set the thread's interruption status to "interrupted", the thread's interruption status will remain unchanged.

what happens if you don't use

Specifically, Thread.currentThread().interrupt()here's what might happen if the method isn't used:

  1. Interrupt request not handled in normal thread:

    • If the thread is running and the interrupt status is not checked, the thread will continue executing without responding to the interrupt request.
    • This may cause the thread to not stop or exit the loop properly, making the application unable to respond to interrupt requests in a timely manner.
  2. Interrupt request not handled in blocking method:

    • If the thread is in a blocked state (such as calling methods such as sleep(), wait(), join()etc.), and InterruptedExceptionthe exception is not caught and processed accordingly, the thread will continue to block.
    • This may cause the thread to not wake up immediately when receiving the interrupt request, and cannot respond to the interrupt in time.

Summary: If you do not use Thread.currentThread().interrupt()the method to set the thread's interrupt status to "interrupted", the thread's interrupt status will remain unchanged. In this case, the thread may continue executing without responding to the interrupt request, resulting in the inability to stop or exit the loop properly, or the inability to respond to the interrupt request in a timely manner while in a blocked state. Therefore, the interrupt status of the thread should be checked at an appropriate time as needed, and handled accordingly.

Guess you like

Origin blog.csdn.net/qq_39017153/article/details/132163611