zoukankan      html  css  js  c++  java
  • 关于InterruptedException





    1.InterruptedException的由来

    "Thread.sleep(long millis)", 以及"thread.wait()"方法,都会抛出InterruptedException,很多时候都不知道怎么处理这个异常;这个异常是由thread.interrupt()产生的,比如如果A线程在wait状态,此时B线程调用aThread.interrupt()方法,那么A线程就会从阻塞状态恢复,并抛出InterruptedException;大部分时候我们我不使用interrupt()方法,所以我们可以直接在抛出InterruptedException的地方捕获住,然后Thread.currentThread().interrupt(), 将interrupted status 置为true就可以了,当然至于interrupted status的状态有什么作用,要这么处理,这是上层代码改决定的事。




    2.interrupt()注释

        /**
         * 中断当前线程;
         *
         * 如果这个线程被wait(), join(), sleep()所阻塞,
         * 那么线程的中断状态将被清除,同时,会抛出一个InterruptedException;
         *
         * <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}.
         * 
         * 如果这个线程是被I/O(基于InterruptibleChannel)操作所阻塞,
         * 那么这个I/O通道教会被关闭,
         * 线程interrupt状态会被设置为true,
         * 同时会抛出一个ClosedByInterruptException;
         * 
         * <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}.
         *
         * 如果这个线程是被link java.nio.channels.Selector所阻塞,
         * 那么这个线程的interrupt状态会被设置为true,
         * 并且会直接从selection操作中return,
         * 就好像select.wakeup()一样;
         *
         * <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.
         *
         * 如果不存在前面所有情况,那么这个线程的interrupt状态将会被设置为true;
         *
         * <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
         */
        //Thread.java
        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();
        }
    
  • 相关阅读:
    KL散度(相对熵)和交叉熵的区别
    将模型从 PyTorch 导出到 ONNX 并使用 ONNX Runtime 运行
    Numpy Boolean Indexing Mask(Numpy 布尔索引掩码 )
    python PIL 图像处理库(Pillow)简介
    YOLO v3 网络结构和源码详解
    PyTorch 下使用 Tensorboard
    Python vars() 函数
    python 的 Tqdm 模块
    Pytorch 中的模式设置:model.eval() 和 model.train()
    Pytorch 中的 zero_grad 使用方法
  • 原文地址:https://www.cnblogs.com/IC1101/p/13279994.html
Copyright © 2011-2022 走看看