5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
+ |
|
9 |
|
import java.util.concurrent.atomic.*; |
10 |
|
import java.util.concurrent.locks.*; |
11 |
|
|
18 |
|
* @since 1.5 |
19 |
|
* @author Doug Lea |
20 |
|
*/ |
20 |
– |
|
21 |
|
public class CancellableTask implements Cancellable, Runnable { |
22 |
|
/** |
23 |
|
* Holds the run-state, taking on values: |
40 |
|
private static final Object CANCELLED = new Object(); |
41 |
|
|
42 |
|
private static AtomicReferenceFieldUpdater<CancellableTask, Object> |
43 |
< |
runnerUpdater = |
44 |
< |
AtomicReferenceFieldUpdater.newUpdater |
45 |
< |
(CancellableTask.class, Object.class, "runner"); |
43 |
> |
runnerUpdater = |
44 |
> |
AtomicReferenceFieldUpdater.newUpdater( |
45 |
> |
CancellableTask.class, |
46 |
> |
Object.class, |
47 |
> |
"runner"); |
48 |
|
|
49 |
|
/** |
50 |
|
* The runnable underlying this task |
72 |
|
protected CancellableTask() { |
73 |
|
} |
74 |
|
|
73 |
– |
|
75 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
76 |
|
Object r = runner; |
77 |
|
if (r == DONE || r == CANCELLED) |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* Return the Runnable forming the basis of this task. |
103 |
> |
* Returns the Runnable forming the basis of this task. |
104 |
|
* @return the runnable action |
105 |
|
* @see #setRunnable |
106 |
|
*/ |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* Set the Runnable forming the basis of this task. |
112 |
> |
* Sets the Runnable forming the basis of this task. |
113 |
|
* @param r the runnable action |
114 |
|
* @see #getRunnable |
115 |
|
*/ |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* Set the state of this task to Cancelled. |
121 |
> |
* Sets the state of this task to Cancelled. |
122 |
|
*/ |
123 |
|
protected void setCancelled() { |
124 |
|
runnerUpdater.set(this, CANCELLED); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* Set the state of this task to Done, unless already |
129 |
< |
* in a Cancelled state, in which Cancelled status is preserved. |
129 |
< |
* |
128 |
> |
* Sets the state of this task to Done, unless already in a |
129 |
> |
* Cancelled state, in which case Cancelled status is preserved. |
130 |
|
*/ |
131 |
|
protected void setDone() { |
132 |
|
for (;;) { |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* Attempt to set the state of this task to Running, succeeding |
144 |
> |
* Attempts to set the state of this task to Running, succeeding |
145 |
|
* only if the state is currently NOT Done, Running, or Cancelled. |
146 |
|
* @return true if successful |
147 |
|
*/ |
174 |
|
protected void done() { } |
175 |
|
|
176 |
|
/** |
177 |
< |
* Reset the run state of this task to its initial state unless |
177 |
> |
* Resets the run state of this task to its initial state unless |
178 |
|
* it has been cancelled. (Note that a cancelled task cannot be |
179 |
|
* reset.) |
180 |
|
* @return true if successful |
230 |
|
private Throwable exception; |
231 |
|
|
232 |
|
/** |
233 |
< |
* Create an InnerCancellableFuture that will execute the |
233 |
> |
* Creates an InnerCancellableFuture that will execute the |
234 |
|
* given callable. |
235 |
|
* @param callable the function to execute |
236 |
|
*/ |