HomeJavaReference Queues | Java

Reference Queues | Java

Reference Queues as the name suggests is a data structure that is used to enqueue the reference objects, such as, WeakReference, SoftReference, and PhantomReference.

Read More: Java Reference Objects and Garbage Collection

Based upon the type of reference object, the exact point when the object will be enqueued varies. The enqueued object also depends on whether the ReferenceQueue object is provided at the time of the creation of the reference object.

Example code for better understanding of the ReferenceQueue and Reference Object creation.

StringBuilder sbObject = new StringBuilder();

ReferenceQueue<StringBuilder> refQueue = new ReferenceQueue<>();

PhantomReference<StringBuilder> sbPhantomRef = new PhantomReference<>(sbObject, refQueue);

sbObject = null

Apart from PhantomReference, it is not mandatory or even useful to provide one.




Most Popular