ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/FutureTask.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/FutureTask.java (file contents):
Revision 1.13 by dl, Thu Dec 4 20:54:29 2003 UTC vs.
Revision 1.14 by dl, Thu Dec 4 21:39:09 2003 UTC

# Line 23 | Line 23 | import java.util.concurrent.locks.*;
23   * implements <tt>Runnable</tt>, a <tt>FutureTask</tt> can be
24   * submitted to an {@link Executor} for execution.
25   *
26 + * <p>In addition to serving as a standalone class, this class provides
27 + * <tt>protected</tt> functionality that may be useful when creating
28 + * customized task classes.
29 + *
30   * @since 1.5
31   * @author Doug Lea
32   * @param <V> The result type returned by this FutureTask's <tt>get</tt> method
# Line 45 | Line 49 | public class FutureTask<V> implements Fu
49       *   DONE              = completed normally,
50       *   CANCELLED         = cancelled (may or may not have ever run).
51       */
48
52      private volatile Object runner;
53 +
54 +    /*
55 +     * For simplicity of use, we can use either a Runnable or a
56 +     * Callable as basis for run method. So one or the other of these
57 +     * will be null.
58 +     */
59 +
60 +    /** The runnable, if non-null, then callable is null */
61      final Runnable runnable;
62 +    /** The callable, if non-null, then runnable is null */
63      final Callable<V> callable;
64 <    private final ReentrantLock lock = new ReentrantLock();
65 <    private final ReentrantLock.ConditionObject accessible = lock.newCondition();
64 >
65 >    /** The result to return from get() */
66      private V result;
67 +    /** The exception to throw from get() */
68      private Throwable exception;
69  
70 +    private final ReentrantLock lock = new ReentrantLock();
71 +    private final ReentrantLock.ConditionObject accessible = lock.newCondition();
72 +
73      /**
74       * Constructs a <tt>FutureTask</tt> that will upon running, execute the
75       * given <tt>Callable</tt>.
# Line 114 | Line 130 | public class FutureTask<V> implements Fu
130      }
131  
132      /**
117     * Sets the state of this task to Cancelled.
118     */
119    protected void setCancelled() {
120        lock.lock();
121        try {
122            runner = CANCELLED;
123        }
124        finally{
125            lock.unlock();
126        }
127    }
128    
129    /**
130     * Sets the state of this task to Done, unless already in a
131     * Cancelled state, in which case Cancelled status is preserved.
132     */
133    protected void setDone() {
134        lock.lock();
135        try {
136            Object r = runner;
137            if (r == DONE || r == CANCELLED)
138                return;
139            runner = DONE;
140        }
141        finally{
142            lock.unlock();
143        }
144        done();
145    }
146
147    /**
148     * Attempts to set the state of this task to Running, succeeding
149     * only if the state is currently NOT Done, Running, or Cancelled.
150     * @return true if successful
151     */
152    protected boolean setRunning() {
153        lock.lock();
154        try {
155            if (runner != null)
156                return false;
157            runner = Thread.currentThread();
158            return true;
159        }
160        finally {
161            lock.unlock();
162        }
163    }
164
165    /**
166     * Sets this Future to the results of computation
167     */
168    public void run() {
169        if (setRunning()) {
170            try {
171                try {
172                    if (runnable != null)
173                        runnable.run();
174                    else if (callable != null)
175                        set(callable.call());
176                } catch(Throwable ex) {
177                    setException(ex);
178                }
179            } finally {
180                setDone();
181            }
182        }
183    }
184
185    /**
186     * Protected method invoked when this task transitions to state
187     * <tt>isDone</tt> (whether normally or via cancellation). The
188     * default implementation does nothing.  Subclasses may override
189     * this method to invoke completion callbacks or perform
190     * bookkeeping. Note that you can query status inside the
191     * implementation of this method to determine whether this task
192     * has been cancelled.
193     */
194    protected void done() { }
195
196    /**
197     * Resets the run state of this task to its initial state unless
198     * it has been cancelled. (Note that a cancelled task cannot be
199     * reset.)
200     * @return true if successful
201     */
202    protected boolean reset() {
203        lock.lock();
204        try {
205            if (runner == CANCELLED)
206                return false;
207            runner = null;
208            return true;
209        }
210        finally {
211            lock.unlock();
212        }
213    }
214
215    /**
133       * Waits if necessary for the call to <tt>callable.call</tt> to
134       * complete, and then retrieves its result.
135       *
# Line 310 | Line 227 | public class FutureTask<V> implements Fu
227          }
228      }
229  
230 +    /**
231 +     * Sets the state of this task to Cancelled.
232 +     */
233 +    protected void setCancelled() {
234 +        lock.lock();
235 +        try {
236 +            runner = CANCELLED;
237 +        }
238 +        finally{
239 +            lock.unlock();
240 +        }
241 +    }
242 +    
243 +    /**
244 +     * Sets the state of this task to Done, unless already in a
245 +     * Cancelled state, in which case Cancelled status is preserved.
246 +     */
247 +    protected void setDone() {
248 +        lock.lock();
249 +        try {
250 +            Object r = runner;
251 +            if (r == DONE || r == CANCELLED)
252 +                return;
253 +            runner = DONE;
254 +        }
255 +        finally{
256 +            lock.unlock();
257 +        }
258 +        done();
259 +    }
260  
261 +    /**
262 +     * Attempts to set the state of this task to Running, succeeding
263 +     * only if the state is currently NOT Done, Running, or Cancelled.
264 +     * @return true if successful
265 +     */
266 +    protected boolean setRunning() {
267 +        lock.lock();
268 +        try {
269 +            if (runner != null)
270 +                return false;
271 +            runner = Thread.currentThread();
272 +            return true;
273 +        }
274 +        finally {
275 +            lock.unlock();
276 +        }
277 +    }
278 +
279 +    /**
280 +     * Sets this Future to the results of computation
281 +     */
282 +    public void run() {
283 +        if (setRunning()) {
284 +            try {
285 +                try {
286 +                    if (runnable != null)
287 +                        runnable.run();
288 +                    else if (callable != null)
289 +                        set(callable.call());
290 +                } catch(Throwable ex) {
291 +                    setException(ex);
292 +                }
293 +            } finally {
294 +                setDone();
295 +            }
296 +        }
297 +    }
298 +
299 +    /**
300 +     * Protected method invoked when this task transitions to state
301 +     * <tt>isDone</tt> (whether normally or via cancellation). The
302 +     * default implementation does nothing.  Subclasses may override
303 +     * this method to invoke completion callbacks or perform
304 +     * bookkeeping. Note that you can query status inside the
305 +     * implementation of this method to determine whether this task
306 +     * has been cancelled.
307 +     */
308 +    protected void done() { }
309 +
310 +    /**
311 +     * Resets the run state of this task to its initial state unless
312 +     * it has been cancelled. (Note that a cancelled task cannot be
313 +     * reset.)
314 +     * @return true if successful
315 +     */
316 +    protected boolean reset() {
317 +        lock.lock();
318 +        try {
319 +            if (runner == CANCELLED)
320 +                return false;
321 +            runner = null;
322 +            return true;
323 +        }
324 +        finally {
325 +            lock.unlock();
326 +        }
327 +    }
328   }
329  
330  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines