java.lang.ref.ReferenceQueue Class in Java
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.
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.
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.
-> Reference Queue Object : java.lang.ref.ReferenceQueue@214c265e -> Reference Queue Poll : null
Please Login to comment...