gpt4 book ai didi

java - 何时通知 ReferenceHandler 线程?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:35:42 28 4
gpt4 key购买 nike

我们知道,线程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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com