当ReferenceHandler线程被通知时会发生什么?

3
我们知道,线程ReferenceHandler负责将待处理的引用实例加入到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);
    }
}
}

如果等待队列为null,则该线程正在等待锁定;我的问题是,何时通知该线程?当挂起的实例被修改时吗?
1个回答

2

从代码中

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

这意味着当收集器完成并需要引用处理程序唤醒时,它将使用notify()进行通知。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接