Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When ReferenceHandler thread is notified?

We know, thread ReferenceHandler is responsible for enqueue pending Reference instance to ReferenceQueue, see this code in 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);
    }
}
}

If pending queue is null, then this thread is waiting on lock;

My question is when this thread is notified? When pending instance is modified?

like image 948
diecui1202 Avatar asked Sep 12 '26 08:09

diecui1202


1 Answers

From the code

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

This implies that the collector will notify() when it has finished and needs the ReferenceHandler to wake up.

like image 64
Peter Lawrey Avatar answered Sep 13 '26 22:09

Peter Lawrey