ReferenceQueue
Stay organized with collections
Save and categorize content based on your preferences.
Reference queues, to which registered reference objects are appended by the
garbage collector after the appropriate reachability changes are detected.
Public Constructor Summary
Public Method Summary
Reference<? extends T>
|
poll()
Polls this queue to see if a reference object is available.
|
Reference<? extends T>
|
remove(long timeout)
Removes the next reference object in this queue, blocking until either
one becomes available or the given timeout period expires.
|
Reference<? extends T>
|
remove()
Removes the next reference object in this queue, blocking until one
becomes available.
|
Inherited Method Summary
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
Public Constructors
public
ReferenceQueue
()
Constructs a new reference-object queue.
Public Methods
public
Reference<? extends T>
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.
Returns
- A reference object, if one was immediately available,
otherwise
null
public
Reference<? extends T>
remove
(long timeout)
Removes the next reference object in this queue, blocking until either
one becomes available or the given timeout period expires.
This method does not offer real-time guarantees: It schedules the
timeout as if by invoking the Object.wait(long)
method.
Parameters
timeout |
If positive, block for up to timeout
milliseconds while waiting for a reference to be
added to this queue. If zero, block indefinitely. |
Returns
- A reference object, if one was available within the specified
timeout period, otherwise
null
public
Reference<? extends T>
remove
()
Removes the next reference object in this queue, blocking until one
becomes available.
Returns
- A reference object, blocking until one becomes available
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eReferenceQueue\u003c/code\u003e is used by the garbage collector to hold registered reference objects after their reachability changes.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods like \u003ccode\u003epoll\u003c/code\u003e, \u003ccode\u003eremove\u003c/code\u003e to retrieve these reference objects from the queue.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003epoll\u003c/code\u003e is non-blocking and returns immediately with a reference object if available or null otherwise.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eremove\u003c/code\u003e is blocking and waits for a reference object to become available, optionally with a timeout.\u003c/p\u003e\n"],["\u003cp\u003eUsing \u003ccode\u003eReferenceQueue\u003c/code\u003e, you can be notified about objects becoming eligible for garbage collection and perform cleanup tasks.\u003c/p\u003e\n"]]],["The `ReferenceQueue` class manages reference objects appended by the garbage collector. It offers a constructor to create a new queue. Key actions include: `poll()`, which checks for and returns an available reference object immediately or returns `null` if none; `remove(long timeout)`, which retrieves a reference object, blocking until one is available or a timeout expires; and `remove()`, which retrieves a reference, blocking until one is available. Several inherited methods are used for creating and comparing `Object`s.\n"],null,["# ReferenceQueue\n\npublic class **ReferenceQueue** extends [Object](../../../../reference/java/lang/Object.html) \nReference queues, to which registered reference objects are appended by the\ngarbage collector after the appropriate reachability changes are detected. \n\n### Public Constructor Summary\n\n|---|---------------------------------------------------------------------------------------------------------------------------------------|\n| | [ReferenceQueue](../../../../reference/java/lang/ref/ReferenceQueue.html#ReferenceQueue())() Constructs a new reference-object queue. |\n\n### Public Method Summary\n\n|--------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Reference](../../../../reference/java/lang/ref/Reference.html)\\\u003c? extends T\\\u003e | [poll](../../../../reference/java/lang/ref/ReferenceQueue.html#poll())() Polls this queue to see if a reference object is available. |\n| [Reference](../../../../reference/java/lang/ref/Reference.html)\\\u003c? extends T\\\u003e | [remove](../../../../reference/java/lang/ref/ReferenceQueue.html#remove(long))(long timeout) Removes the next reference object in this queue, blocking until either one becomes available or the given timeout period expires. |\n| [Reference](../../../../reference/java/lang/ref/Reference.html)\\\u003c? extends T\\\u003e | [remove](../../../../reference/java/lang/ref/ReferenceQueue.html#remove())() Removes the next reference object in this queue, blocking until one becomes available. |\n\n### Inherited Method Summary\n\nFrom class [java.lang.Object](../../../../reference/java/lang/Object.html) \n\n|----------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Object](../../../../reference/java/lang/Object.html) | [clone](../../../../reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |\n| boolean | [equals](../../../../reference/java/lang/Object.html#equals(java.lang.Object))([Object](../../../../reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |\n| void | [finalize](../../../../reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |\n| final [Class](../../../../reference/java/lang/Class.html)\\\u003c?\\\u003e | [getClass](../../../../reference/java/lang/Object.html#getClass())() Returns the unique instance of [Class](../../../../reference/java/lang/Class.html) that represents this object's class. |\n| int | [hashCode](../../../../reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |\n| final void | [notify](../../../../reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |\n| final void | [notifyAll](../../../../reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |\n| [String](../../../../reference/java/lang/String.html) | [toString](../../../../reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |\n| final void | [wait](../../../../reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |\n| final void | [wait](../../../../reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |\n| final void | [wait](../../../../reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |\n\nPublic Constructors\n-------------------\n\n#### public\n**ReferenceQueue**\n()\n\nConstructs a new reference-object queue.\n\nPublic Methods\n--------------\n\n#### public [Reference](../../../../reference/java/lang/ref/Reference.html)\\\u003c? extends T\\\u003e\n**poll**\n()\n\nPolls this queue to see if a reference object is available. If one is\navailable without further delay then it is removed from the queue and\nreturned. Otherwise this method immediately returns null. \n\n##### Returns\n\n- A reference object, if one was immediately available, otherwise `null` \n\n#### public [Reference](../../../../reference/java/lang/ref/Reference.html)\\\u003c? extends T\\\u003e\n**remove**\n(long timeout)\n\nRemoves the next reference object in this queue, blocking until either\none becomes available or the given timeout period expires.\n\nThis method does not offer real-time guarantees: It schedules the\ntimeout as if by invoking the [Object.wait(long)](../../../../reference/java/lang/Object.html#wait(long)) method. \n\n##### Parameters\n\n| timeout | If positive, block for up to `timeout` milliseconds while waiting for a reference to be added to this queue. If zero, block indefinitely. |\n|---------|-------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- A reference object, if one was available within the specified timeout period, otherwise `null` \n\n##### Throws\n\n| [IllegalArgumentException](../../../../reference/java/lang/IllegalArgumentException.html) | If the value of the timeout argument is negative |\n| [InterruptedException](../../../../reference/java/lang/InterruptedException.html) | If the timeout wait is interrupted |\n|-------------------------------------------------------------------------------------------|--------------------------------------------------|\n\n#### public [Reference](../../../../reference/java/lang/ref/Reference.html)\\\u003c? extends T\\\u003e\n**remove**\n()\n\nRemoves the next reference object in this queue, blocking until one\nbecomes available. \n\n##### Returns\n\n- A reference object, blocking until one becomes available \n\n##### Throws\n\n| [InterruptedException](../../../../reference/java/lang/InterruptedException.html) | If the wait is interrupted |\n|-----------------------------------------------------------------------------------|----------------------------|"]]