java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > interrupt方法

Java线程中的interrupt方法解读

作者:huskyui

这篇文章主要介绍了Java线程中的interrupt方法解读,Java中的interrupt是一种线程间通信的机制,用于请求中断线程的执行。当一个线程调用另一个线程的interrupt()方法时,被调用线程会收到一个中断信号,可以根据需要做出相应的处理,需要的朋友可以参考下

interrupt方法

首先梳理Thread关于interrupt的定义

/**
 * Interrupts this thread.
 *
 * <p> Unless the current thread is interrupting itself, which is
 * always permitted, the {@link #checkAccess() checkAccess} method
 * of this thread is invoked, which may cause a {@link
 * SecurityException} to be thrown.
 *
 * <p> If this thread is blocked in an invocation of the {@link
 * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 * class, or of the {@link #join()}, {@link #join(long)}, {@link
 * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
 * methods of this class, then its interrupt status will be cleared and it
 * will receive an {@link InterruptedException}.
 *
 * <p> If this thread is blocked in an I/O operation upon an {@link
 * java.nio.channels.InterruptibleChannel InterruptibleChannel}
 * then the channel will be closed, the thread's interrupt
 * status will be set, and the thread will receive a {@link
 * java.nio.channels.ClosedByInterruptException}.
 *
 * <p> If this thread is blocked in a {@link java.nio.channels.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 {@link
 * java.nio.channels.Selector#wakeup wakeup} method were invoked.
 *
 * <p> If none of the previous conditions hold then this thread's interrupt
 * status will be set. </p>
 *
 * <p> Interrupting a thread that is not alive need not have any effect.
 *
 * @throws  SecurityException
 *          if the current thread cannot modify this thread
 *
 * @revised 6.0
 * @spec JSR-51
 */
public void interrupt() 

这里讲述了Thread实例对象调用方法interrupt,有这几种情况

? 1.在线程里面有wait,sleep,join方法,会弹出InterruptedException,状态会被清除

? 2.io里面,会弹出异常,状态会被设置,…

? 3.如果线程阻塞在nio中的Selector中,状态会被设置,…

? 4.如果不是上面,这些情况,状态会被设置

可以看出interrupt是关于状态的设置

我们来验证一下

第一种,没有任何情况的

public static void main(String[] args) {
    Thread t1 = new Thread(()->{
       while(true){
           if (interrupted()) {
               break;
           }
           System.out.println("i" + new Date());
       }
    });
    t1.start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t1.interrupt();
}

线程会在(1000+)ms后结束,整个程序也会结束

第二种,有sleep的情况下

public static void main(String[] args) {
    Thread t1 = new Thread(()->{
       while(true){
           if (interrupted()) {
               break;
           }
           try {
               Thread.sleep(100);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }

           System.out.println("i" + new Date());
       }
    });
    t1.start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t1.interrupt();
}

方法并不会退出,只是打出了一个InterruptException

如何处理中断?

更新相关interrupt方法的意义

public void interrupt()

Thread里面的打断函数,可以使用新建一个Thread对象,执行thread.interrupt();

修改调用线程的interrupt 的status

public static boolean interrupted()

该方法可以判断是否被打断,但是这个interrupt的status会被清除,

/**
     * Tests whether the current thread has been interrupted.  The
     * <i>interrupted status</i> of the thread is cleared by this method.  In
     * other words, if this method were to be called twice in succession, the
     * second call would return false (unless the current thread were
     * interrupted again, after the first call had cleared its interrupted
     * status and before the second call had examined it).
     *
     * <p>A thread interruption ignored because a thread was not alive
     * at the time of the interrupt will be reflected by this method
     * returning false.
     *
     * @return  <code>true</code> if the current thread has been interrupted;
     *          <code>false</code> otherwise.
     * @see #isInterrupted()
     * @revised 6.0
     */
public boolean isInterrupted()

该方法可以判断是否被打断

到此这篇关于Java线程中的interrupt方法解读的文章就介绍到这了,更多相关interrupt方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文