首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaLangAccess.blockedOn(线程t,可中断b)是做什么的?

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

Stack Overflow用户
提问于 2011-12-17 12:55:42
回答 2查看 868关注 0票数 1

来自公共javadoc:

void blockedOn(线程t,可中断b)

设置线程的阻止程序字段。

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

EN

回答 2

Stack Overflow用户

发布于 2011-12-17 13:11:09

如果您查看它调用的OpenJDK

代码语言:javascript
复制
/* 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
EN

Stack Overflow用户

发布于 2011-12-17 13:00:39

似乎,我在java.lang.Thread源代码(Oracle)中找到了答案:

代码语言:javascript
复制
/* 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();
}

所以如果我错了,我的结论是:

sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(threadInstance,intrInstance);只对线程中断事件

  • 可中断事件进行回调,
  • 只能通过以下代码将具体的可中断实例附加到具体的类中:
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8544891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档