Journal for Developers

Reference Queues | Java

0 48

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

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.

 

Subscribe to our Journal

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!