176 |
|
return report(s); |
177 |
|
} |
178 |
|
|
179 |
+ |
@Override |
180 |
+ |
public V resultNow() { |
181 |
+ |
switch (state()) { // Future.State |
182 |
+ |
case SUCCESS: |
183 |
+ |
@SuppressWarnings("unchecked") |
184 |
+ |
V result = (V) outcome; |
185 |
+ |
return result; |
186 |
+ |
case FAILED: |
187 |
+ |
throw new IllegalStateException("Task completed with exception"); |
188 |
+ |
case CANCELLED: |
189 |
+ |
throw new IllegalStateException("Task was cancelled"); |
190 |
+ |
default: |
191 |
+ |
throw new IllegalStateException("Task has not completed"); |
192 |
+ |
} |
193 |
+ |
} |
194 |
+ |
|
195 |
+ |
@Override |
196 |
+ |
public Throwable exceptionNow() { |
197 |
+ |
switch (state()) { // Future.State |
198 |
+ |
case SUCCESS: |
199 |
+ |
throw new IllegalStateException("Task completed with a result"); |
200 |
+ |
case FAILED: |
201 |
+ |
Object x = outcome; |
202 |
+ |
return (Throwable) x; |
203 |
+ |
case CANCELLED: |
204 |
+ |
throw new IllegalStateException("Task was cancelled"); |
205 |
+ |
default: |
206 |
+ |
throw new IllegalStateException("Task has not completed"); |
207 |
+ |
} |
208 |
+ |
} |
209 |
+ |
|
210 |
+ |
@Override |
211 |
+ |
public State state() { |
212 |
+ |
int s = state; |
213 |
+ |
while (s == COMPLETING) { |
214 |
+ |
// waiting for transition to NORMAL or EXCEPTIONAL |
215 |
+ |
Thread.yield(); |
216 |
+ |
s = state; |
217 |
+ |
} |
218 |
+ |
switch (s) { |
219 |
+ |
case NORMAL: |
220 |
+ |
return State.SUCCESS; |
221 |
+ |
case EXCEPTIONAL: |
222 |
+ |
return State.FAILED; |
223 |
+ |
case CANCELLED: |
224 |
+ |
case INTERRUPTING: |
225 |
+ |
case INTERRUPTED: |
226 |
+ |
return State.CANCELLED; |
227 |
+ |
default: |
228 |
+ |
return State.RUNNING; |
229 |
+ |
} |
230 |
+ |
} |
231 |
+ |
|
232 |
|
/** |
233 |
|
* Protected method invoked when this task transitions to state |
234 |
|
* {@code isDone} (whether normally or via cancellation). The |