what is the use of the list to hold PhantomReferences
<p>//main method</p>

<pre><code>public class PhantomReferenceUsage 
{

public static void main(String[] args) 
{
    ReferenceQueue queue = new ReferenceQueue();

    List&lt;MyPhantomReference&gt; l = new PhantomReferenceUsage().createObj(queue);

    System.gc();  

    Thread t = new Thread(new MyTask(queue, l));
    t.start();
}

private List&lt;MyPhantomReference&gt; createObj(ReferenceQueue queue)
{
    MyClass m1 = new MyClass(10);
    MyPhantomReference mp1 = new MyPhantomReference(m1, queue);


    MyClass m2 = new MyClass(11);
    MyPhantomReference mp2 = new MyPhantomReference(m2, queue);

    MyClass m3 = new MyClass(12);
    MyPhantomReference mp3 = new MyPhantomReference(m3, queue);


   List&lt;MyPhantomReference&gt; l = new ArrayList&lt;MyPhantomReference&gt;();
   l.add(mp1);
   l.add(mp2);
   l.add(mp3);

   return l;
}
</code></pre>

<p>}</p>

<p>//creating MyPhantomReference to hold Myclass</p>

<pre><code>public class MyPhantomReference extends PhantomReference
{
private final MyClass m;

public MyPhantomReference(MyClass mc, ReferenceQueue rq) 
{
    super(mc, rq);
    this.m = mc;
}

public void cleanup() 
{    
    System.out.println("in MyPhantomReference "+m.getI());
    m.close();     
}
</code></pre>

<p>}</p>

<p>//running the cleanup in different thread </p>

<pre><code>public class MyTask implements Runnable
{
private final ReferenceQueue rf;
private final List&lt;MyPhantomReference&gt; list;

public MyTask(ReferenceQueue r, List&lt;MyPhantomReference&gt; l)
{
    rf = r;
    list = l;
}

@Override
public void run() 
{
    while(true)
    {
        try
        {
            MyPhantomReference m = (MyPhantomReference)rf.remove();
            m.cleanup();
            list.remove(m);
        } 
        catch (InterruptedException ex)
        {
            Logger.getLogger(MyTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
</code></pre>

<p>}</p>

<p>If I will not use the </p>

<pre><code>List&lt;MyPhantomReference&gt; l = new ArrayList&lt;MyPhantomReference&gt;(); 
</code></pre>

<p>and simply get the MyPhantomReference from ReferenceQueue and call  cleanup(); then whats the difference. </p>

<p>Thanks!!! Hope now i am clear and met the standard.</p>
