package phanRef;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;

public class PhantomReferences {

    public static void main(String[] args){
        ReferenceQueue<Person> queue = new ReferenceQueue<Person>();
        ArrayList<FinalizePerson> list = new ArrayList<FinalizePerson>();
        ArrayList<Person> people = new ArrayList<Person>();

        for(int i = 0; i<10;i++){
            Person p  = new Person();
            people.add(p);
            list.add(new FinalizePerson(p, queue));

        }
        people = null;
        System.gc();

        for (PhantomReference<Person> reference : list){
            System.out.println(reference.isEnqueued());
        }

    }


}

class FinalizePerson extends PhantomReference<Person>{
    public FinalizePerson(Person referent, ReferenceQueue<? super Person> q){
        super(referent, q);
    }

    public void cleanup(){System.out.println("person is finalizing resources");}
}

class Person{}

@@ -50,16 +50,48 @@
 erence;%0A
+import java.lang.ref.Reference;%0A
 import j
@@ -757,24 +757,251 @@
 %0A        %7D%0A%0A
+        Reference%3C? extends Person%3E referenceFromQueue;%0A        while((referenceFromQueue = queue.poll()) != null)%7B%0A            ((FinalizePerson)referenceFromQueue).cleanup();%0A            referenceFromQueue.clear();%0A        %7D%0A%0A
     %7D%0A%0A%0A%7D%0A%0Ac

check if phantom references were cleared.