jdk/src/share/classes/java/lang/ref/package.html
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
<!--
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
<html>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
<body bgcolor="white">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
Provides reference-object classes, which support a limited degree of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
interaction with the garbage collector.  A program may use a reference object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
to maintain a reference to some other object in such a way that the latter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
object may still be reclaimed by the collector.  A program may also arrange to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
be notified some time after the collector has determined that the reachability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
of a given object has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
<h2>Package Specification</h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
A <em>reference object</em> encapsulates a reference to some other object so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
that the reference itself may be examined and manipulated like any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
object.  Three types of reference objects are provided, each weaker than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
last: <em>soft</em>, <em>weak</em>, and <em>phantom</em>.  Each type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
corresponds to a different level of reachability, as defined below.  Soft
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
references are for implementing memory-sensitive caches, weak references are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
for implementing canonicalizing mappings that do not prevent their keys (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
values) from being reclaimed, and phantom references are for scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
pre-mortem cleanup actions in a more flexible way than is possible with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
Java finalization mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
<p> Each reference-object type is implemented by a subclass of the abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
base <code>{@link java.lang.ref.Reference}</code> class.  An instance of one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
these subclasses encapsulates a single reference to a particular object, called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
the <em>referent</em>.  Every reference object provides methods for getting and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
clearing the reference.  Aside from the clearing operation reference objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
are otherwise immutable, so no <code>set</code> operation is provided.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
program may further subclass these subclasses, adding whatever fields and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
methods are required for its purposes, or it may use these subclasses without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
<h3>Notification</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
A program may request to be notified of changes in an object's reachability by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
<em>registering</em> an appropriate reference object with a <em>reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
queue</em> at the time the reference object is created.  Some time after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
garbage collector determines that the reachability of the referent has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
to the value corresponding to the type of the reference, it will add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
reference to the associated queue.  At this point, the reference is considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
to be <em>enqueued</em>.  The program may remove references from a queue either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
by polling or by blocking until a reference becomes available.  Reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
queues are implemented by the <code>{@link java.lang.ref.ReferenceQueue}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
<p> The relationship between a registered reference object and its queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
one-sided.  That is, a queue does not keep track of the references that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
registered with it.  If a registered reference becomes unreachable itself, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
it will never be enqueued.  It is the responsibility of the program using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
reference objects to ensure that the objects remain reachable for as long as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
the program is interested in their referents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
<p> While some programs will choose to dedicate a thread to removing reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
objects from one or more queues and processing them, this is by no means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
necessary.  A tactic that often works well is to examine a reference queue in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
the course of performing some other fairly-frequent action.  For example, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
hashtable that uses weak references to implement weak keys could poll its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
reference queue each time the table is accessed.  This is how the <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
java.util.WeakHashMap}</code> class works.  Because the <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
java.lang.ref.ReferenceQueue#poll ReferenceQueue.poll}</code> method simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
checks an internal data structure, this check will add little overhead to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
hashtable access methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
<h3>Automatically-cleared references</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
Soft and weak references are automatically cleared by the collector before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
being added to the queues with which they are registered, if any.  Therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
soft and weak references need not be registered with a queue in order to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
useful, while phantom references do.  An object that is reachable via phantom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
references will remain so until all such references are cleared or themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
become unreachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
<a name="reachability"></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
<h3>Reachability</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
Going from strongest to weakest, the different levels of reachability reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
the life cycle of an object.  They are operationally defined as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
<li> An object is <em>strongly reachable</em> if it can be reached by some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
thread without traversing any reference objects.  A newly-created object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
strongly reachable by the thread that created it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
<li> An object is <em>softly reachable</em> if it is not strongly reachable but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
can be reached by traversing a soft reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
<li> An object is <em>weakly reachable</em> if it is neither strongly nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
softly reachable but can be reached by traversing a weak reference.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
weak references to a weakly-reachable object are cleared, the object becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
eligible for finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
<li> An object is <em>phantom reachable</em> if it is neither strongly, softly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
nor weakly reachable, it has been finalized, and some phantom reference refers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
<li> Finally, an object is <em>unreachable</em>, and therefore eligible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
reclamation, when it is not reachable in any of the above ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
@author	  Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
@since	  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
<!--
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
<h2>Related Documentation</h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
For overviews, tutorials, examples, guides, and tool documentation, please see:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  <li><a href="">##### REFER TO NON-SPEC DOCUMENTATION HERE #####</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
</body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
</html>