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.120 by jsr166, Sun Mar 11 18:00:06 2018 UTC vs.
Revision 1.121 by dl, Fri May 6 16:03:05 2022 UTC

# Line 176 | Line 176 | public class FutureTask<V> implements Ru
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines