1 19 20 package org.netbeans.modules.projectapi; 21 22 27 import java.lang.ref.WeakReference ; 28 import org.openide.util.RequestProcessor; 29 import org.openide.util.Utilities; 30 31 49 public final class TimedWeakReference<T> extends WeakReference <T> implements Runnable { 50 51 public static int TIMEOUT = 15000; 52 53 private static final RequestProcessor RP = new RequestProcessor("TimedWeakReference"); 55 private RequestProcessor.Task task; 56 57 private T o; 58 59 60 private long touched; 61 62 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 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 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 |