BS问题:Thread.interrupt()方法

目录

1、interrupt的中文意思

2、方法注解的翻译

3、自己的理解


1、interrupt的中文意思

vt.& vi.  打断(别人的话等); 阻止; 截断;
vt.  暂停; 中断; 打扰; 妨碍;
vi.  打断;
n.  中断; 暂停;

不管是及物动词还是不及物动词亦或是名称都是中断、暂停的意思

第一个问题:那么在Thread.interrupt()中,是不是意味着暂停当前线程呢?是的

第二个问题:如果是暂停当前线程,那么当前线程必须是在运行状态?不是,如果当前线程阻塞了,会清空中断标识,而且抛出异常

2、方法注解的翻译

    /**
     * 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.
     * 除非当前线程正在中断它自己,线程自己中断自己是允许的,调用会抛出SecurityException异常的        
       checkAccess()方法
     * <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}.
     * 如果当前线程正在阻塞中,因为调用了Object对象的wait()方法,wait(long)方法,wait(long,     
       int),或者Thread类的join()方法,join(long)方法,join(long, int)方法,sleep(long)方        
       法,或者sleep(long, int) ,那么当前线程的中断状态会被清除,以及它当前线程会收到    
       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}.
       在可中断的通道上面,如果当前线程在一个I/O操作中处于阻塞状态,然后将关闭通道,设置线程的中 
       断状态,而且线程会收到一个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() {
        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();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值