1 19 package org.openide.util; 20 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.logging.Level ; 24 25 26 45 public class Task extends Object implements Runnable { 46 47 public static final Task EMPTY = new Task(); 48 49 static { 50 EMPTY.finished = true; 51 } 52 53 55 private static java.util.WeakHashMap <Class , Boolean > overrides; 56 57 60 private static RequestProcessor RP; 61 62 63 final Runnable run; 64 65 66 private boolean finished; 67 68 69 private HashSet <TaskListener> list; 70 71 76 public Task(Runnable run) { 77 this.run = run; 78 79 if (run == null) { 80 finished = true; 81 } 82 } 83 84 88 protected Task() { 89 this.run = null; 90 } 91 92 95 public final boolean isFinished() { 96 synchronized (this) { 97 return finished; 98 } 99 } 100 101 104 public void waitFinished() { 105 synchronized (this) { 106 while (!finished) { 107 try { 108 wait(); 109 } catch (InterruptedException ex) { 110 } 111 } 112 } 113 } 114 115 122 public boolean waitFinished(long milliseconds) throws InterruptedException { 123 synchronized (this) { 124 if (overridesTimeoutedWaitFinished()) { 125 if (finished) { 129 return true; 130 } 131 132 long expectedEnd = System.currentTimeMillis() + milliseconds; 133 134 for (;;) { 135 wait(milliseconds); 136 137 if (finished) { 138 return true; 139 } 140 141 long now = System.currentTimeMillis(); 142 143 if (expectedEnd <= now) { 144 return false; 145 } 146 147 milliseconds = expectedEnd - now; 148 } 149 } 150 } 151 152 class Run implements Runnable { 158 public void run() { 159 Task.this.waitFinished(); 160 } 161 } 162 163 RequestProcessor.Task task = RP.post(new Run()); 164 165 return task.waitFinished(milliseconds); 166 } 167 168 172 protected final void notifyRunning() { 173 synchronized (this) { 174 if (RequestProcessor.logger().isLoggable(Level.FINE)) { 175 RequestProcessor.logger().fine("notifyRunning: " + this); } 177 this.finished = false; 178 notifyAll(); 179 } 180 } 181 182 185 protected final void notifyFinished() { 186 Iterator it; 187 188 synchronized (this) { 189 finished = true; 190 if (RequestProcessor.logger().isLoggable(Level.FINE)) { 191 RequestProcessor.logger().fine("notifyFinished: " + this); } 193 notifyAll(); 194 195 if (list == null) { 197 return; 198 } 199 200 it = ((HashSet ) list.clone()).iterator(); 201 } 202 203 while (it.hasNext()) { 204 TaskListener l = (TaskListener) it.next(); 205 l.taskFinished(this); 206 } 207 } 208 209 217 public void run() { 218 try { 219 notifyRunning(); 220 221 if (run != null) { 222 run.run(); 223 } 224 } finally { 225 notifyFinished(); 226 } 227 } 228 229 232 public synchronized void addTaskListener(TaskListener l) { 233 if (list == null) { 234 list = new HashSet <TaskListener>(); 235 } 236 237 list.add(l); 238 239 if (finished) { 240 l.taskFinished(this); 241 } 242 } 243 244 247 public synchronized void removeTaskListener(TaskListener l) { 248 if (list == null) { 249 return; 250 } 251 252 list.remove(l); 253 } 254 255 public String toString() { 256 return "task " + run; } 258 259 261 private boolean overridesTimeoutedWaitFinished() { 262 if (getClass() == Task.class) { 264 return true; 265 } 266 267 if (getClass() == RequestProcessor.Task.class) { 269 return true; 270 } 271 272 java.util.WeakHashMap <Class ,Boolean > m; 273 Boolean does; 274 275 synchronized (Task.class) { 276 if (overrides == null) { 277 overrides = new java.util.WeakHashMap <Class , Boolean >(); 278 RP = new RequestProcessor("Timeout waitFinished compatibility processor", 255); } 280 281 m = overrides; 282 283 does = m.get(getClass()); 284 285 if (does != null) { 286 return does.booleanValue(); 287 } 288 289 try { 290 java.lang.reflect.Method method = getClass().getMethod("waitFinished", new Class [] { Long.TYPE }); does = Boolean.valueOf(method.getDeclaringClass() != Task.class); 292 m.put(getClass(), does); 293 294 return does.booleanValue(); 295 } catch (Exception ex) { 296 Exceptions.printStackTrace(ex); 297 298 return true; 299 } 300 } 301 } 302 303 306 String debug() { 307 return (run == null) ? "null" : run.getClass().getName(); 308 } 309 } 310 | Popular Tags |