|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.util.WeakReferenceMonitor
public class WeakReferenceMonitor
Track references to arbitrary objects using proxy and weak references. To
monitor a handle, one should call monitor(Object, ReleaseListener)
,
with the given handle object usually being a holder that uses the target
object underneath, and the release listener performing cleanup of the
target object once the handle is not strongly referenced anymore.
When a given handle becomes weakly reachable, the specified listener will be called by a background thread. This thread will only be started lazily and will be stopped once no handles are registered for monitoring anymore, to be restarted if further handles are added.
Thanks to Tomasz Wysocki for the suggestion and the original implementation of this class!
monitor(java.lang.Object, org.springframework.util.WeakReferenceMonitor.ReleaseListener)
Nested Class Summary | |
---|---|
private static class |
WeakReferenceMonitor.MonitoringProcess
Thread implementation that performs the actual monitoring. |
static interface |
WeakReferenceMonitor.ReleaseListener
Listener that is notified when the handle is being released. |
Field Summary | |
---|---|
private static java.lang.ref.ReferenceQueue<java.lang.Object> |
handleQueue
|
private static Log |
logger
|
private static java.lang.Thread |
monitoringThread
|
private static java.util.Map<java.lang.ref.Reference,WeakReferenceMonitor.ReleaseListener> |
trackedEntries
|
Constructor Summary | |
---|---|
WeakReferenceMonitor()
|
Method Summary | |
---|---|
private static void |
addEntry(java.lang.ref.Reference ref,
WeakReferenceMonitor.ReleaseListener entry)
Add entry to internal map of tracked entries. |
private static boolean |
keepMonitoringThreadAlive()
Check whether to keep the monitoring thread alive, i.e. |
static void |
monitor(java.lang.Object handle,
WeakReferenceMonitor.ReleaseListener listener)
Start to monitor given handle object for becoming weakly reachable. |
private static WeakReferenceMonitor.ReleaseListener |
removeEntry(java.lang.ref.Reference reference)
Remove entry from internal map of tracked entries. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Log logger
private static final java.lang.ref.ReferenceQueue<java.lang.Object> handleQueue
private static final java.util.Map<java.lang.ref.Reference,WeakReferenceMonitor.ReleaseListener> trackedEntries
private static java.lang.Thread monitoringThread
Constructor Detail |
---|
public WeakReferenceMonitor()
Method Detail |
---|
public static void monitor(java.lang.Object handle, WeakReferenceMonitor.ReleaseListener listener)
handle
- the object that will be monitoredlistener
- the listener that will be called upon release of the handleprivate static void addEntry(java.lang.ref.Reference ref, WeakReferenceMonitor.ReleaseListener entry)
ref
- reference to tracked handleentry
- the associated entryprivate static WeakReferenceMonitor.ReleaseListener removeEntry(java.lang.ref.Reference reference)
reference
- the reference that should be removed
private static boolean keepMonitoringThreadAlive()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |