關於PhantomReference研究的一個問題

package com.sunhang;

//: containers/References.java
// Demonstrates Reference objects
import java.lang.ref.*;
import java.util.*;

class VeryBig {
    private static final int SIZE = 10000;
    private long[] la = new long[SIZE];
    private String ident;
    public VeryBig(String id) { ident = id; }
    public String toString() { return ident; }
    protected void finalize() {
        System.out.println("Finalizing " + ident);
    }
}

public class References {
    private static ReferenceQueue<VeryBig> rq =
            new ReferenceQueue<VeryBig>();
    public static void checkQueue() {
//        Reference<? extends VeryBig> inq = rq.poll();
//        if(inq != null)
//            System.out.println("In queue: " + inq.get());
    }
    public static void main(String[] args) throws InterruptedException {
        int size = 10;
        // Or, choose size via the command line:
        if(args.length > 0)
            size = new Integer(args[0]);
        LinkedList<SoftReference<VeryBig>> sa =
                new LinkedList<SoftReference<VeryBig>>();
        for(int i = 0; i < size; i++) {
            sa.add(new SoftReference<VeryBig>(
                    new VeryBig("Soft " + i), rq));
            System.out.println("Just created: " + sa.getLast());
            checkQueue();
        }
        LinkedList<WeakReference<VeryBig>> wa =
                new LinkedList<WeakReference<VeryBig>>();
        for(int i = 0; i < size; i++) {
            wa.add(new WeakReference<VeryBig>(
                    new VeryBig("Weak " + i), rq));
            System.out.println("Just created: " + wa.getLast());
            checkQueue();
        }
        SoftReference<VeryBig> s =
                new SoftReference<VeryBig>(new VeryBig("Soft"));
        WeakReference<VeryBig> w =
                new WeakReference<VeryBig>(new VeryBig("Weak"));

        System.gc();

        LinkedList<PhantomReference<VeryBig>> pa =
                new LinkedList<PhantomReference<VeryBig>>();
        for(int i = 0; i < size; i++) {
            pa.add(new PhantomReference<VeryBig>(
                    new VeryBig("Phantom " + i), rq));
            System.out.println("Just created: " + pa.getLast());
            checkQueue();
        }


        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Reference<? extends VeryBig> inq = null;
                int count = 0;
//                while((inq = rq.poll()) != null){
//                    count ++;
//                    System.out.println("inq:" + inq.get() + " count:" + count + " " + inq.getClass().getSimpleName());
//                }

                synchronized (Reference.class){
                    try {
                        Reference.class.wait(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println("again");


                count = 0;
                while((inq = rq.poll()) != null){
                    count ++;
                    System.out.println("inq:" + inq.get() + " count:" + count + " " + inq.getClass().getSimpleName());
                }

            }
        }.start();

        Thread.sleep(3000);
        System.gc();

        Thread.sleep(15000);
    }
} /* (Execute to see output) *///:~

這段代碼我認爲會把PhantomReference加入到ReferenceQueue中,可是結果中表示並無加入到隊列中。這是爲何呢?有機會研究一下?結果以下:java


Just created: java.lang.ref.SoftReference@ab612f8ide

Just created: java.lang.ref.SoftReference@24e5ddd03d

Just created: java.lang.ref.SoftReference@6f45959ccode

Just created: java.lang.ref.SoftReference@41616dd6隊列

Just created: java.lang.ref.SoftReference@63721e22get

Just created: java.lang.ref.SoftReference@986b0eeit

Just created: java.lang.ref.SoftReference@69c9de24io

Just created: java.lang.ref.SoftReference@441743beast

Just created: java.lang.ref.SoftReference@536091declass

Just created: java.lang.ref.SoftReference@6908b095

Just created: java.lang.ref.WeakReference@251e9862

Just created: java.lang.ref.WeakReference@67219b8c

Just created: java.lang.ref.WeakReference@663d0426

Just created: java.lang.ref.WeakReference@2fdb8f3a

Just created: java.lang.ref.WeakReference@77984161

Just created: java.lang.ref.WeakReference@2cec849e

Just created: java.lang.ref.WeakReference@5bf6b81c

Just created: java.lang.ref.WeakReference@4ffb06c9

Just created: java.lang.ref.WeakReference@697a9f24

Just created: java.lang.ref.WeakReference@71662a95

Just created: java.lang.ref.PhantomReference@7c669255

Finalizing Weak

Finalizing Weak 9

Finalizing Weak 8

Finalizing Weak 7

Finalizing Weak 6

Finalizing Weak 5

Finalizing Weak 4

Finalizing Weak 3

Finalizing Weak 2

Finalizing Weak 1

Finalizing Weak 0

Just created: java.lang.ref.PhantomReference@32114a41

Just created: java.lang.ref.PhantomReference@d220b15

Just created: java.lang.ref.PhantomReference@3605966f

Just created: java.lang.ref.PhantomReference@28d76d1e

Just created: java.lang.ref.PhantomReference@573ce184

Just created: java.lang.ref.PhantomReference@5df9cdda

Just created: java.lang.ref.PhantomReference@3b39d369

Just created: java.lang.ref.PhantomReference@5166b0df

Just created: java.lang.ref.PhantomReference@2ce62a39

Finalizing Phantom 4

Finalizing Phantom 5

Finalizing Phantom 7

Finalizing Phantom 6

Finalizing Phantom 0

Finalizing Phantom 1

Finalizing Phantom 2

Finalizing Phantom 3

Finalizing Phantom 9

Finalizing Phantom 8

again

inq:null count:1 WeakReference

inq:null count:2 WeakReference

inq:null count:3 WeakReference

inq:null count:4 WeakReference

inq:null count:5 WeakReference

inq:null count:6 WeakReference

inq:null count:7 WeakReference

inq:null count:8 WeakReference

inq:null count:9 WeakReference

inq:null count:10 WeakReference