Open In App

java.lang.ref.ReferenceQueue Class in Java

Last Updated : 09 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

java.lang.ref.ReferenceQueue Class in Java

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




// Java Program to illustrate ReferenceQueue Class
  
// Importing classes from package- java.lang.ref
// to make a deal between program with garbage collector
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
  
// Class 1
// Helper class
class HelperClass {
  
    // Method of this class
    void Display()
    {
        // Print statement whenever
        // function of HelperClass is called
        System.out.println(
            "HelperClass Display() invoked...");
    }
}
  
// Class 2
// Main class
class GFG {
  
    // Main drive method
    public static void main(String[] args)
    {
  
        // Creating new object of HelperClass
        HelperClass obj = new HelperClass();
  
        // Creating Reference queue of HelperClass
        ReferenceQueue<HelperClass> rq
            = new ReferenceQueue<>();
  
        // Creating Phantom reference
        WeakReference<HelperClass> wr
            = new WeakReference<>(obj);
  
        // Display message
        System.out.println("-> Reference Queue Object :");
  
        // Printing reference queue object
        System.out.println(rq);
  
        // Display message
        System.out.println("-> Reference Queue Poll :");
  
        // Checking if phantom object is lined up
        // or cleared in the queue
        // using the poll() method
        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

Similar Reads