1 19 20 package org.netbeans.modules.enode; 21 22 import java.lang.ref.SoftReference ; 23 import java.util.Map ; 24 25 import org.openide.util.*; 26 27 49 public final class TimedSoftReference extends SoftReference implements Runnable { 50 51 private static final int TIMEOUT = 30000; 52 53 private static final RequestProcessor RP = new RequestProcessor("enode-TimedSoftReference"); 55 private RequestProcessor.Task task; 56 57 private Object o; 58 59 private final Map m; 60 61 private final Object k; 62 63 64 private long touched; 65 66 74 public TimedSoftReference(Object o, Map m, Object k) { 75 super(o, Utilities.activeReferenceQueue()); 76 this.o = o; 77 this.m = m; 78 this.k = k; 79 task = RP.create(this); 80 task.schedule(TIMEOUT); 81 } 82 83 public void run() { 84 synchronized (m) { 85 if (o != null) { 86 long unused = System.currentTimeMillis() - touched; 89 if (unused > TIMEOUT/2) { 90 o = null; 91 touched = 0; 92 } else { 93 task.schedule(TIMEOUT - (int)unused); 94 } 95 } else { 96 m.remove(k); 99 } 100 } 101 } 102 103 public Object get() { 104 synchronized (m) { 105 if (o == null) { 106 o = super.get(); 107 } 108 if (o != null) { 109 if (touched == 0) { 112 task.schedule(TIMEOUT); 113 } 114 touched = System.currentTimeMillis(); 115 return o; 116 } else { 117 return null; 118 } 119 } 120 } 121 122 } 123 | Popular Tags |