1 19 package org.openide.util; 20 21 import java.lang.ref.SoftReference ; 22 23 import java.util.Map ; 24 25 26 46 final class TimedSoftReference<T> extends SoftReference <T> implements Runnable { 47 private static final int TIMEOUT = 30000; 48 private static final RequestProcessor RP = new RequestProcessor("TimedSoftReference"); private RequestProcessor.Task task; 50 private T o; 51 private final Map m; 52 private final Object k; 53 54 55 private long touched; 56 57 65 public TimedSoftReference(T o, Map m, Object k) { 66 super(o, Utilities.activeReferenceQueue()); 67 this.o = o; 68 this.m = m; 69 this.k = k; 70 task = RP.create(this); 71 task.schedule(TIMEOUT); 72 } 73 74 public void run() { 75 synchronized (m) { 76 if (o != null) { 77 long unused = System.currentTimeMillis() - touched; 80 81 if (unused > (TIMEOUT / 2)) { 82 o = null; 83 touched = 0; 84 } else { 85 task.schedule(TIMEOUT - (int) unused); 86 } 87 } else { 88 m.remove(k); 91 } 92 } 93 } 94 95 public T get() { 96 synchronized (m) { 97 if (o == null) { 98 o = super.get(); 99 } 100 101 if (o != null) { 102 if (touched == 0) { 105 task.schedule(TIMEOUT); 106 } 107 108 touched = System.currentTimeMillis(); 109 110 return o; 111 } else { 112 return null; 113 } 114 } 115 } 116 } 117 | Popular Tags |