Lines 41-46
Link Here
|
41 |
static { |
41 |
static { |
42 |
EMPTY.finished = true; |
42 |
EMPTY.finished = true; |
43 |
} |
43 |
} |
|
|
44 |
/** map of subclasses to booleans whether they override waitFinished() or not |
45 |
* <Class, Boolean> |
46 |
*/ |
47 |
private static java.util.WeakHashMap overrides; |
48 |
/** request processor for workarounding compatibility problem with |
49 |
* classes that do not override waitFinished (long) |
50 |
*/ |
51 |
private static RequestProcessor RP; |
44 |
|
52 |
|
45 |
/** what to run */ |
53 |
/** what to run */ |
46 |
private Runnable run; |
54 |
private Runnable run; |
Lines 89-94
Link Here
|
89 |
} |
97 |
} |
90 |
} |
98 |
} |
91 |
|
99 |
|
|
|
100 |
|
101 |
/** Wait until the task is finished, but only a given time. |
102 |
* @param milliseconds time in milliseconds to wait for the result |
103 |
* @exception InterruptedException when the waiting has been interrupted |
104 |
* @return true if the task is really finished, or false if the time out |
105 |
* has been exceeded |
106 |
* @since JST-PENDING |
107 |
*/ |
108 |
public boolean waitFinished (long milliseconds) throws InterruptedException { |
109 |
synchronized (this) { |
110 |
if (overridesTimeoutedWaitFinished ()) { |
111 |
// the the task overrides waitFinished (timeout) or is |
112 |
// one of the basic tasks, then we can just simply do our bese |
113 |
// code. Otherwise we have to execute threading workaround |
114 |
if (finished) return true; |
115 |
|
116 |
long expectedEnd = System.currentTimeMillis () + milliseconds; |
117 |
for (;;) { |
118 |
wait (milliseconds); |
119 |
|
120 |
if (finished) return true; |
121 |
long now = System.currentTimeMillis (); |
122 |
|
123 |
if (expectedEnd <= now) return false; |
124 |
|
125 |
milliseconds = expectedEnd - now; |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
|
131 |
// as we know that RequestProcessor implements the waitFinished(long) |
132 |
// correctly we just post a task for waitFinished() into some |
133 |
// of its threads and wait just the given milliseconds time |
134 |
// for the result, by that we can guarantee the semantics |
135 |
// of the call |
136 |
class Run implements Runnable { |
137 |
public void run () { |
138 |
Task.this.waitFinished (); |
139 |
} |
140 |
} |
141 |
RequestProcessor.Task task = RP.post (new Run ()); |
142 |
return task.waitFinished (milliseconds); |
143 |
} |
144 |
|
92 |
/** Changes the state of the task to be running. Any call after this |
145 |
/** Changes the state of the task to be running. Any call after this |
93 |
* one and before notifyFinished to waitFinished blocks. |
146 |
* one and before notifyFinished to waitFinished blocks. |
94 |
* @since 1.5 |
147 |
* @since 1.5 |
Lines 157-165
Link Here
|
157 |
if (list == null) return; |
210 |
if (list == null) return; |
158 |
list.remove (l); |
211 |
list.remove (l); |
159 |
} |
212 |
} |
160 |
|
213 |
|
161 |
public String toString () { |
214 |
public String toString () { |
162 |
return "task " + run; // NOI18N |
215 |
return "task " + run; // NOI18N |
|
|
216 |
} |
217 |
|
218 |
/** Checks whether the class overrides wait finished. |
219 |
*/ |
220 |
private boolean overridesTimeoutedWaitFinished () { |
221 |
// yes we implement it corretly |
222 |
if (getClass () == Task.class) return true; |
223 |
// RequestProcessor.Task overrides correctly |
224 |
if (getClass () == RequestProcessor.Task.class) return true; |
225 |
|
226 |
java.util.WeakHashMap m; |
227 |
Boolean does; |
228 |
synchronized (Task.class) { |
229 |
if (overrides == null) { |
230 |
overrides = new java.util.WeakHashMap (); |
231 |
RP = new RequestProcessor ("Timeout waitFinished compatibility processor", 255); // NOI18N |
232 |
} |
233 |
m = overrides; |
234 |
|
235 |
does = (Boolean)m.get (getClass ()); |
236 |
if (does != null) return does.booleanValue (); |
237 |
|
238 |
try { |
239 |
java.lang.reflect.Method method = getClass ().getMethod ("waitFinished", new Class[] { Long.TYPE }); // NOI18N |
240 |
does = Boolean.valueOf (method.getDeclaringClass () != Task.class); |
241 |
m.put (getClass (), does); |
242 |
return does.booleanValue (); |
243 |
} catch (Exception ex) { |
244 |
org.openide.ErrorManager.getDefault ().notify (ex); |
245 |
return true; |
246 |
} |
247 |
} |
163 |
} |
248 |
} |
164 |
|
249 |
|
165 |
/** Reveal the identity of the worker runnable. |
250 |
/** Reveal the identity of the worker runnable. |