Reference.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* java.lang.ref.Reference
  2. Copyright (C) 1999, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang.ref;
  32. /**
  33. * This is the base class of all references. A reference allows
  34. * refering to an object without preventing the garbage collector to
  35. * collect it. The only way to get the referred object is via the
  36. * <code>get()</code>-method. This method will return
  37. * <code>null</code> if the object was collected. <br>
  38. *
  39. * A reference may be registered with a queue. When a referred
  40. * element gets collected the reference will be put on the queue, so
  41. * that you will be notified. <br>
  42. *
  43. * There are currently three types of references: soft reference,
  44. * weak reference and phantom reference. <br>
  45. *
  46. * Soft references will be cleared if the garbage collector is told
  47. * to free some memory and there are no unreferenced or weakly referenced
  48. * objects. It is useful for caches. <br>
  49. *
  50. * Weak references will be cleared as soon as the garbage collector
  51. * determines that the refered object is only weakly reachable. They
  52. * are useful as keys in hashtables (see <code>WeakHashtable</code>) as
  53. * you get notified when nobody has the key anymore.
  54. *
  55. * Phantom references don't prevent finalization. If an object is only
  56. * phantom reachable, it will be finalized, and the reference will be
  57. * enqueued, but not cleared. Since you mustn't access an finalized
  58. * object, the <code>get</code> method of a phantom reference will never
  59. * work. It is useful to keep track, when an object is finalized.
  60. *
  61. * @author Jochen Hoenicke
  62. * @see java.util.WeakHashMap
  63. */
  64. public abstract class Reference<T>
  65. {
  66. /**
  67. * The underlying object. This field is handled in a special way by
  68. * the garbage collector.
  69. */
  70. T referent;
  71. /**
  72. * The queue this reference is registered on. This is null, if this
  73. * wasn't registered to any queue or reference was already enqueued.
  74. */
  75. volatile ReferenceQueue<? super T> queue;
  76. /**
  77. * Link to the next entry on the queue. If this is null, this
  78. * reference is not enqueued. Otherwise it points to the next
  79. * reference. The last reference on a queue will point to itself
  80. * (not to null, that value is used to mark a not enqueued
  81. * reference).
  82. */
  83. volatile Reference nextOnQueue;
  84. /**
  85. * This lock should be taken by the garbage collector, before
  86. * determining reachability. It will prevent the get()-method to
  87. * return the reference so that reachability doesn't change.
  88. */
  89. static Object lock = new Object();
  90. /**
  91. * Creates a new reference that is not registered to any queue.
  92. * Since it is package private, it is not possible to overload this
  93. * class in a different package.
  94. * @param ref the object we refer to.
  95. */
  96. Reference(T ref)
  97. {
  98. referent = ref;
  99. }
  100. /**
  101. * Creates a reference that is registered to a queue. Since this is
  102. * package private, it is not possible to overload this class in a
  103. * different package.
  104. * @param ref the object we refer to.
  105. * @param q the reference queue to register on.
  106. * @exception NullPointerException if q is null.
  107. */
  108. Reference(T ref, ReferenceQueue<? super T> q)
  109. {
  110. if (q == null)
  111. throw new NullPointerException();
  112. referent = ref;
  113. queue = q;
  114. }
  115. /**
  116. * Returns the object, this reference refers to.
  117. * @return the object, this reference refers to, or null if the
  118. * reference was cleared.
  119. */
  120. public T get()
  121. {
  122. synchronized (lock)
  123. {
  124. return referent;
  125. }
  126. }
  127. /**
  128. * Clears the reference, so that it doesn't refer to its object
  129. * anymore. For soft and weak references this is called by the
  130. * garbage collector. For phantom references you should call
  131. * this when enqueuing the reference.
  132. */
  133. public void clear()
  134. {
  135. referent = null;
  136. }
  137. /**
  138. * Tells if the object is enqueued on a reference queue.
  139. * @return true if it is enqueued, false otherwise.
  140. */
  141. public boolean isEnqueued()
  142. {
  143. return nextOnQueue != null;
  144. }
  145. /**
  146. * Enqueue an object on a reference queue. This is normally executed
  147. * by the garbage collector.
  148. */
  149. public boolean enqueue()
  150. {
  151. ReferenceQueue q = queue;
  152. if (q != null)
  153. {
  154. return q.enqueue(this);
  155. }
  156. return false;
  157. }
  158. }