e108. determining when an object will be reclaimed.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 19 行

TXT
19
字号
A phantom reference is used to determine when an object is just about to be reclaimed. Phantom references are safer to use than finalization because once an object is phantom reachable, it cannot be resurrected. 
    // Create the phantom reference.
    ReferenceQueue rq = new ReferenceQueue();
    PhantomReference pr = new PhantomReference(object, rq);
    
    // Wait until the object is about to be reclaimed.
    try {
        while (true) {
            Reference r = rq.remove();
            if (r == pr) {
                // The object is about to be reclaimed.
                // Clear the referent so that it can be reclaimed.
                r.clear();
            }
        }
    } catch (InterruptedException e) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?