java.lang.ref.ReferenceQueue Class in Java
Last Updated :
09 Mar, 2021
A ReferenceQueue is a simple data structure onto which the garbage collector places reference objects when the reference field is cleared (set to null). You would use a reference queue to find out when an object becomes softly, weakly, or phantom reachable, so your program can take some action based on that knowledge. For example, a program might perform some post-finalization cleanup processing that requires an object to be unreachable (such as the reallocation of resources outside the Java heap) upon learning that an object has become phantom reachable.
A ReferenceQueue can be used to find out if an object is weak, soft, or phantom unreachable. Now getting a dig deeper let’s define the constructor invoked here in this class as follows:
public ReferenceQueue(): It constructs a new reference-object queue.
Furthermore, let do define the methods in ReferenceQueue Class
Method Name |
Method Description |
poll() |
Polls this queue to see if a reference object is available. If one is available without further delay then it is removed from the queue and returned. Otherwise, this method immediately returns null. |
remove() |
Removes the next reference object in this queue, blocking until one becomes available. |
remove(long TimeOut) |
Removes the next reference object in this queue, blocking until either one becomes available or the given timeout period expires. |
Let’s discuss the above methods in detail individually as follows:
Method 1: The poll() method of Queue Interface returns and removes the element at the front of the container. It deletes the element in the container. The method does not throw an exception when the Queue is empty, it returns null instead.
Syntax:
E poll()
Returns: This method returns the element at the front of the container or the head of the Queue. It returns null when the Queue is empty.
Method 2: The remove() method of Queue Interface returns and removes the element at the front of the container. It deletes the head of the container. The method throws an exception known as NoSuchElementException when the queue is empty.
Syntax:
E remove()
Returns: This method returns the head of the Queue.
Exception: The function throws an NoSuchElementException when the Queue is empty.
By now we all done discussing the major methods been here in this class. Let dive straight away into the implementation of these methods in this class.
Example:
Java
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
class HelperClass {
void Display()
{
System.out.println(
"HelperClass Display() invoked..." );
}
}
class GFG {
public static void main(String[] args)
{
HelperClass obj = new HelperClass();
ReferenceQueue<HelperClass> rq
= new ReferenceQueue<>();
WeakReference<HelperClass> wr
= new WeakReference<>(obj);
System.out.println( "-> Reference Queue Object :" );
System.out.println(rq);
System.out.println( "-> Reference Queue Poll :" );
System.out.println(rq.poll());
}
}
|
Output
-> Reference Queue Object :
java.lang.ref.ReferenceQueue@214c265e
-> Reference Queue Poll :
null
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...