“Java中断及中断响应”的版本间差异

来自Wikioe
跳到导航 跳到搜索
第5行: 第5行:
Java的中断为协作机制,即 A 不能直接中断 B,而是调用 “B.interrupt()” 来申请中断 B (设置中断状态);然后由 B 通过 “interrupted()”(会重置中断状态)或 “isInterrupted()”(不会重置中断状态)读取中断状态,并决定如何响应。
Java的中断为协作机制,即 A 不能直接中断 B,而是调用 “B.interrupt()” 来申请中断 B (设置中断状态);然后由 B 通过 “interrupted()”(会重置中断状态)或 “isInterrupted()”(不会重置中断状态)读取中断状态,并决定如何响应。
</pre>
</pre>
Java的中断:
# 没有可以强制线程终止的方法。
# interrupt 方法用于'''请求终止线程'''。


=== 中断相关源码 ===
=== 中断相关源码 ===
第59行: 第55行:
* 中断标识的使用,都是在native方法中。
* 中断标识的使用,都是在native方法中。


== 请求中断 ==
Java的中断:
# 没有可以强制线程终止的方法。
# interrupt 方法用于'''请求终止线程'''。


== 中断响应 ==
== 中断响应 ==

2020年10月19日 (一) 22:56的版本


中断机制

Java的中断为协作机制,即 A 不能直接中断 B,而是调用 “B.interrupt()” 来申请中断 B (设置中断状态);然后由 B 通过 “interrupted()”(会重置中断状态)或 “isInterrupted()”(不会重置中断状态)读取中断状态,并决定如何响应。

中断相关源码

	private volatile Interruptible blocker;				// 
    private final Object blockerLock = new Object();	// 用于synchronized锁定,实现同步安全
	
	/* Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code
     */
	void blockedOn(Interruptible b) {
        synchronized (blockerLock) {
            blocker = b;
        }
    }
	
	...
	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);		// 调用Interruptible对象b 的同步方法
                return;
            }
        }
        interrupt0();
    }
	
	public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }
	
	public boolean isInterrupted() {
        return isInterrupted(false);
    }
	
	private native boolean isInterrupted(boolean ClearInterrupted);
	...
	private native void interrupt0();

其中:

  1. “interrupt()” 方法只是调用了“blocker.interrupt()”和“interrupt0()”,并不对线程运行产生影响;
  2. “blocker”域用于 nio 的中断?【???待查证】
  3. “interrupt0()” 是一个 native方法,仅用于设置中断标识(其他的);
  4. “interrupted()”(静态方法)和“isInterrupted()”(实例方法)最终都是调用本地方法“isInterrupted”;
  5. “isInterrupted”是一个 native方法,用于判断中断标识,并根据参数决定是否重置中断状态;
  • 中断标识的使用,都是在native方法中。

请求中断

Java的中断:

  1. 没有可以强制线程终止的方法。
  2. interrupt 方法用于请求终止线程

中断响应

Java 中,使用“interrupt()”方法请求中断,并不会改变线程运行状态,只是改变了线程中断信号。 响应中断的两种方式:

  1. 对于不响应中断的方法,需要在
  2. 响应中断的方法,会抛出InterruptedException异常(并重置中断标识),处理该异常即可;


当线程处在不同的状态下(Running,Blocked,Waiting),响应中断信号的方式不一样:

  1. 非阻塞:在需要处理中断逻辑的地方,利用“interrupted”或“isInterrupted”方法判断中断标示,并编写处理代码,以响应处理中断;
  2. 阻塞:JVM会让线程马上抛出异常(InterruptedException)并把中断状态置回为false,从而让线程可以选择是否响应中断;
     /*
     * ...
     *
     * <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}.
     *
     * ...
     */

响应中断的方法

  1. 响应中断的方法: 线程进入等待或是超时等待的状态后,调用interrupt方法都是会响应中断的;
    所以响应中断的方法:Object.wait()、Thread.join、Thread.sleep、LockSupport.park等方法。
  2. 不响应中断的方法:线程进入阻塞状态后,以及阻塞在ReentrantLock.lock方法里面的线程,是不响应中断的。
    等待进入synchronized的方法或是代码块,都是会被阻塞的,此时不会响应中断;
    可以使用“ReentrantLock.lockInterruptibly”方法响应中断;

InterruptedException 处理

如果抛出 InterruptedException 意味着一个方法是阻塞方法,那么调用一个阻塞方法则意味着您的方法也是一个阻塞方法,而且您应该有某种策略来处理 InterruptedException。

对于 InterruptedException

  1. 最糟糕的处理方式是 “生吞(swallow)”:即,捕捉它,然后什么也不做(或者记录下它)。