java - 何时通知 ReferenceHandler 线程?

标签 java reference jvm

我们知道,线程ReferenceHandler负责将待处理的Reference实例入队到ReferenceQueue,参见Reference$ReferenceHandler.run()中的这段代码:

public void run() {
    for (;;) {

    Reference r;
    synchronized (lock) {
        if (pending != null) {
        r = pending;
        Reference rn = r.next;
        pending = (rn == r) ? null : rn;
        r.next = r;
        } else {
        try {
            lock.wait();
        } catch (InterruptedException x) { }
        continue;
        }
    }

    // Fast path for cleaners
    if (r instanceof Cleaner) {
        ((Cleaner)r).clean();
        continue;
    }

    ReferenceQueue q = r.queue;
    if (q != ReferenceQueue.NULL) q.enqueue(r);
    }
}
}

如果pending queue为null,那么这个线程正在等待lock

我的问题是什么时候通知这个线程?待定实例何时被修改?

最佳答案

来自代码

/* Object used to synchronize with the garbage collector.  The collector
 * must acquire this lock at the beginning of each collection cycle.  It is
 * therefore critical that any code holding this lock complete as quickly
 * as possible, allocate no new objects, and avoid calling user code.
 */
static private class Lock { };
private static Lock lock = new Lock();

这意味着收集器将在完成并需要 ReferenceHandler 唤醒时 notify()

关于java - 何时通知 ReferenceHandler 线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8181347/

相关文章:

java - Eclipse 无法创建可运行 JAR 文件

java - 将节点列表的元素扔到堆栈上

在子类中使用 "super"并在父类(super class)中使用 "this"的 Java 运行时方法选择

java - 如何诊断无休止的 JVM GC 运行?

java - java中验证字符串只包含某些字符

c - 未定义对 main 的引用 - collect2 : ld returned 1 exit status

c++ - map 和 unordered_map 包含指向 double 的指针?

c++ - 引用变量在 C++ 中不返回值

java - 关于静态方法和调用堆栈的问题

java - 将 mojo 生成的代码动态添加到源路径