Saturday, 26 July 2014

enqueue() : Add reference object to the queue

public boolean enqueue()
Return true if this reference object was successfully enqueued; false if it was already enqueued or if it was not registered with a queue when it was created.

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

public class ReferenceEnque {
    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.out.println(weakRef.enqueue());
        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
true
Is Reference object enqueued true
1 Krishna





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment