Saturday 26 July 2014

Reference Queue

Sometimes it is important to know which references the garbage collector has cleared. By using ReferenceQueue, you can know which references are cleared. All you have to do is pass a ReferenceQueue object while creating the reference.

ReferenceQueue refQueue = new ReferenceQueue();
Object obj = new Object();
WeakReference sr = new WeakReference(obj,refQueue);
obj = null;

Methods in Reference Queue
Method Description
poll() If the reference is available in the queue, then it return and remove the elements from queue. If no reference is available, then it returns null.
remove() Return the next available enqueued reference on the queue, blocking indefinately until one is available.
remove(long timeout) Removes the next reference object in this queue, blocking until either one becomes available or the given timeout period expires.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment