Source Code Cross Referenced for SoftReference.java in  » 6.0-JDK-Core » lang » java » lang » ref » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » lang » java.lang.ref 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.lang.ref;
027
028        /**
029         * Soft reference objects, which are cleared at the discretion of the garbage
030         * collector in response to memory demand.  Soft references are most often used
031         * to implement memory-sensitive caches.
032         *
033         * <p> Suppose that the garbage collector determines at a certain point in time
034         * that an object is <a href="package-summary.html#reachability">softly
035         * reachable</a>.  At that time it may choose to clear atomically all soft
036         * references to that object and all soft references to any other
037         * softly-reachable objects from which that object is reachable through a chain
038         * of strong references.  At the same time or at some later time it will
039         * enqueue those newly-cleared soft references that are registered with
040         * reference queues.
041         *
042         * <p> All soft references to softly-reachable objects are guaranteed to have
043         * been cleared before the virtual machine throws an
044         * <code>OutOfMemoryError</code>.  Otherwise no constraints are placed upon the
045         * time at which a soft reference will be cleared or the order in which a set
046         * of such references to different objects will be cleared.  Virtual machine
047         * implementations are, however, encouraged to bias against clearing
048         * recently-created or recently-used soft references.
049         *
050         * <p> Direct instances of this class may be used to implement simple caches;
051         * this class or derived subclasses may also be used in larger data structures
052         * to implement more sophisticated caches.  As long as the referent of a soft
053         * reference is strongly reachable, that is, is actually in use, the soft
054         * reference will not be cleared.  Thus a sophisticated cache can, for example,
055         * prevent its most recently used entries from being discarded by keeping
056         * strong referents to those entries, leaving the remaining entries to be
057         * discarded at the discretion of the garbage collector.
058         *
059         * @version  1.41, 05/05/07
060         * @author   Mark Reinhold
061         * @since    1.2
062         */
063
064        public class SoftReference<T> extends Reference<T> {
065
066            /* Timestamp clock, updated by the garbage collector
067             */
068            static private long clock;
069
070            /* Timestamp updated by each invocation of the get method.  The VM may use
071             * this field when selecting soft references to be cleared, but it is not
072             * required to do so.
073             */
074            private long timestamp;
075
076            /**
077             * Creates a new soft reference that refers to the given object.  The new
078             * reference is not registered with any queue.
079             *
080             * @param referent object the new soft reference will refer to
081             */
082            public SoftReference(T referent) {
083                super (referent);
084                this .timestamp = clock;
085            }
086
087            /**
088             * Creates a new soft reference that refers to the given object and is
089             * registered with the given queue.
090             *
091             * @param referent object the new soft reference will refer to
092             * @param q the queue with which the reference is to be registered,
093             *          or <tt>null</tt> if registration is not required
094             *
095             */
096            public SoftReference(T referent, ReferenceQueue<? super  T> q) {
097                super (referent, q);
098                this .timestamp = clock;
099            }
100
101            /**
102             * Returns this reference object's referent.  If this reference object has
103             * been cleared, either by the program or by the garbage collector, then
104             * this method returns <code>null</code>.
105             *
106             * @return   The object to which this reference refers, or
107             *           <code>null</code> if this reference object has been cleared
108             */
109            public T get() {
110                T o = super.get();
111                if (o != null)
112                    this.timestamp = clock;
113                return o;
114            }
115
116        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.