JavaLangAccess.blockedOn(线程t,可中断的b)做什么?

时间:2011-12-17 12:55:42

标签: java jvm nio

来自public javadoc:

void blockedOn(Thread t, Interruptible b)
     

设置线程的阻止字段。

我在java nio研究期间使用该方法进行了堆栈,特别是AbstractInterruptibleChannel源代码

2 个答案:

答案 0 :(得分:1)

如果查看OpenJDK,则调用

/* The object in which this thread is blocked in an interruptible I/O
 * operation, if any.  The blocker's interrupt method should be invoked
 * after setting this thread's interrupt status.
 */
private volatile Interruptible blocker;
private Object blockerLock = new Object();

/* Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code
 */
void blockedOn(Interruptible b) {
synchronized (blockerLock) {
    blocker = b;
}
}

这用于在线程中断时触发操作。

答案 1 :(得分:0)

好像,我确实在java.lang.Thread源代码(Oracle / Sun JVM)中找到了答案:

/* The object in which this thread is blocked in an interruptible I/O
 * operation, if any.  The blocker's interrupt method should be invoked
 * after setting this thread's interrupt status.
 */
private volatile Interruptible blocker;
private Object blockerLock = new Object();

/* 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();
        return;
        }
    }
    interrupt0();
}

如果我错了,请纠正我,我的结论是:

  • Interruptible只是线程中断事件的回调
  • 只能使用以下代码将具体的可中断实例附加到具体类:sun.misc.SharedSecrets.getJavaLangAccess()。blockedOn(threadInstance,intrInstance);