KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectapi > TimedWeakReference


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectapi;
21
22 // XXX COPIED from org.openide.util w/ changes:
23
// weak -> soft
24
// timeout
25
// removed map key functionality
26

27 import java.lang.ref.WeakReference JavaDoc;
28 import org.openide.util.RequestProcessor;
29 import org.openide.util.Utilities;
30
31 /**
32  * A weak reference which is held strongly for a while after last access.
33  * Lifecycle:
34  * <ol>
35  * <li>Created. Referent held strongly. A task is scheduled into the request
36  * processor for some time in the future (currently 15 seconds).</li>
37  * <li>Expired. After the timeout, the reference switches to a normal weak
38  * reference.</li>
39  * <li>Touched. If the value is accessed before it is garbage collected,
40  * whether the reference is expired or not, the reference is "touched".
41  * This means that the referent is again held strongly and the timeout
42  * is started from scratch.</li>
43  * <li>Dead. If after expiry there is no access before the next full GC cycle,
44  * the GC algorithm may reclaim the reference. In this case the reference
45  * of course dies.</li>
46  * </ol>
47  * @author Jesse Glick
48  */

49 public final class TimedWeakReference<T> extends WeakReference JavaDoc<T> implements Runnable JavaDoc {
50     
51     public static int TIMEOUT = 15000;
52     
53     private static final RequestProcessor RP = new RequestProcessor("TimedWeakReference"); // NOI18N
54

55     private RequestProcessor.Task task;
56     
57     private T o;
58     
59     /** Time when the object was last time touched */
60     private long touched;
61     
62     /**
63      * Create a weak reference with timeout.
64      * @param o the referent
65      */

66     public TimedWeakReference(T o) {
67         super(o, Utilities.activeReferenceQueue());
68         this.o = o;
69         task = RP.create(this);
70         task.schedule(TIMEOUT);
71     }
72     
73     public synchronized void run() {
74         if (o != null) {
75             //System.err.println("Expire " + k);
76
// how long we've really been idle
77
long unused = System.currentTimeMillis() - touched;
78             if (unused > TIMEOUT / 2) {
79                 o = null;
80                 touched = 0;
81             } else {
82                 task.schedule(TIMEOUT - (int) unused);
83             }
84         }
85     }
86     
87     public synchronized T get() {
88         if (o == null) {
89             o = super.get();
90         }
91         if (o != null) {
92             // touch me
93
//System.err.println("Touch " + k);
94
if (touched == 0) {
95                 task.schedule(TIMEOUT);
96             }
97             touched = System.currentTimeMillis();
98             return o;
99         } else {
100             return null;
101         }
102     }
103     
104 }
105
Popular Tags