Saturday, 26 July 2014

isEnqueued() : Check whether this reference object en queued

public boolean isEnqueued()
Returns true if the reference object en queued to the queue with which it is registered.

import java.lang.ref.*;

public class CheckEnqueue {
    static class Employee{
        String name;
        int id;

        Employee(int id, String name){
            this.id =id;
            this.name = name;
        }
        
        @Override
        public String toString(){
            return id +" " + name;
        }
    }

    public static void main(String args[]) throws InterruptedException{
        
        Employee emp1 = new Employee(1, "Krishna");
        ReferenceQueue refQ = new ReferenceQueue();
        
        WeakReference<Employee> weakRef;
        weakRef = new WeakReference<> (emp1, refQ);
        
        System.out.println(weakRef.get());
        System.out.print("Is Reference object enqueued ");
        System.out.println(weakRef.isEnqueued());
        
        emp1 = null;
        System.gc();
        Thread.sleep(2000);
        System.out.print("Is Reference object enqueued ");
        System.out.println(weakRef.isEnqueued());
        System.out.println(weakRef.get());        
    }
}


Output
1 Krishna
Is Reference object enqueued false
Is Reference object enqueued true
null






 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment