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