1. /*
  2. * @(#)PhantomReference.java 1.19 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.ref;
  8. /**
  9. * Phantom reference objects, which are enqueued after the collector
  10. * determines that their referents may otherwise be reclaimed. Phantom
  11. * references are most often used for scheduling pre-mortem cleanup actions in
  12. * a more flexible way than is possible with the Java finalization mechanism.
  13. *
  14. * <p> If the garbage collector determines at a certain point in time that the
  15. * referent of a phantom reference is <a
  16. * href="package-summary.html#reachability">phantom reachable</a>, then at that
  17. * time or at some later time it will enqueue the reference.
  18. *
  19. * <p> In order to ensure that a reclaimable object remains so, the referent of
  20. * a phantom reference may not be retrieved: The <code>get</code> method of a
  21. * phantom reference always returns <code>null</code>.
  22. *
  23. * <p> Unlike soft and weak references, phantom references are not
  24. * automatically cleared by the garbage collector as they are enqueued. An
  25. * object that is reachable via phantom references will remain so until all
  26. * such references are cleared or themselves become unreachable.
  27. *
  28. * @version 1.19, 12/19/03
  29. * @author Mark Reinhold
  30. * @since 1.2
  31. */
  32. public class PhantomReference<T> extends Reference<T> {
  33. /**
  34. * Returns this reference object's referent. Because the referent of a
  35. * phantom reference is always inaccessible, this method always returns
  36. * <code>null</code>.
  37. *
  38. * @return <code>null</code>
  39. */
  40. public T get() {
  41. return null;
  42. }
  43. /**
  44. * Creates a new phantom reference that refers to the given object and
  45. * is registered with the given queue.
  46. *
  47. * <p> It is possible to create a phantom reference with a <tt>null</tt>
  48. * queue, but such a reference is completely useless: Its <tt>get</tt>
  49. * method will always return null and, since it does not have a queue, it
  50. * will never be enqueued.
  51. *
  52. * @param referent the object the new phantom reference will refer to
  53. * @param q the queue with which the reference is to be registered,
  54. * or <tt>null</tt> if registration is not required
  55. */
  56. public PhantomReference(T referent, ReferenceQueue<? super T> q) {
  57. super(referent, q);
  58. }
  59. }