public static Integer doTest() throws ExecutionException, InterruptedException { System.out.println("Hello world1!"); Fiber<Integer> integerFiber = new Fiber<Integer>() { @Override protected Integer run() throws SuspendExecution, InterruptedException { System.out.println("Hello world2!"); return 10; } }.start(); System.out.println("Hello world3!"); return integerFiber.get(); } }
public static Integer doTest() throws ExecutionException, InterruptedException { System.out.println("Hello world1!"); Fiber<Integer> integerFiber = new Fiber<Integer>() { @Override protected Integer run() throws SuspendExecution, InterruptedException { System.out.println("Hello world2!"); Fiber.sleep(1000); System.out.println("Hello world3!"); return 2; } }.start(); System.out.println("Hello world4!"); Integer result = integerFiber.get(); System.out.println("Hello world5!"); return result; } }
fiber2.join(); return fiber1.get();
@Override public Object get() throws ExecutionException, InterruptedException { return fiber.get(); }
@Override public Object get(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { return fiber.get(timeout, unit); }
@Override public final void join() throws ExecutionException, InterruptedException { get(); }
@Override public final void join(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { get(timeout, unit); }
@Override @Suspendable public final void join() throws ExecutionException, InterruptedException { get(); }
@Override public V get() throws InterruptedException, ExecutionException { if (strand instanceof Fiber) return ((Fiber<V>) strand).get(); else { strand.join(); return result; } }
@Override @Suspendable public final void join(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { get(timeout, unit); }
@Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { if (strand instanceof Fiber) return ((Fiber<V>) strand).get(timeout, unit); else { strand.join(timeout, unit); return result; } }
/** * Blocks on the input fibers and creates a new list from the results. The result list is the same order as the * input list. * * @param fibers to combine */ public static <V> List<V> get(final List<Fiber<V>> fibers) throws InterruptedException { final List<V> results = new ArrayList<>(fibers.size()); //TODO on interrupt, should all input fibers be canceled? for (final Fiber<V> f : fibers) { try { results.add(f.get()); } catch (ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } } return Collections.unmodifiableList(results); }
/** * Runs an action in a new fiber, awaits the fiber's termination, and returns its result. * * @param <V> * @param scheduler the {@link FiberScheduler} to use when scheduling the fiber. * @param target the operation * @return the operations return value * @throws ExecutionException * @throws InterruptedException */ public static <V> V runInFiber(FiberScheduler scheduler, SuspendableCallable<V> target) throws ExecutionException, InterruptedException { return new Fiber<V>(scheduler, target).start().get(); }
/** * Blocks on the input fibers and creates a new list from the results. The result list is the same order as the * input list. * * @param timeout to wait for all requests to complete * @param unit the time is in * @param fibers to combine */ public static <V> List<V> get(long timeout, TimeUnit unit, List<Fiber<V>> fibers) throws InterruptedException, TimeoutException { if (unit == null) return get(fibers); if (timeout < 0) timeout = 0; final List<V> results = new ArrayList<>(fibers.size()); long left = unit.toNanos(timeout); final long deadline = System.nanoTime() + left; //TODO on interrupt, should all input fibers be canceled? try { for (final Fiber<V> f : fibers) { if (left >= 0) { results.add(f.get(left, TimeUnit.NANOSECONDS)); left = deadline - System.nanoTime(); } else throw new TimeoutException("timed out sequencing fiber results"); } return Collections.unmodifiableList(results); } catch (ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } }
@Override @Suspendable public final V get() throws InterruptedException, ExecutionException { final Strand s = strand; if (s == null) throw new IllegalStateException("Actor strand not set (not started?)"); if (s instanceof Fiber) return ((Fiber<V>) s).get(); else { s.join(); return actor.getResult(); } }
@Override @Suspendable public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { if (strand instanceof Fiber) return ((Fiber<V>) strand).get(timeout, unit); else { strand.join(timeout, unit); return actor.getResult(); } }
/** * Runs an action in a new fiber, awaits the fiber's termination, and returns its result. * Unlike {@link #runInFiber(FiberScheduler, SuspendableCallable) runInFiber} this method does not throw {@link ExecutionException}, but wraps * any checked exception thrown by the operation in a {@link RuntimeException}. * * @param <V> * @param scheduler the {@link FiberScheduler} to use when scheduling the fiber. * @param target the operation * @return the operations return value * @throws InterruptedException */ public static <V> V runInFiberRuntime(FiberScheduler scheduler, SuspendableCallable<V> target) throws InterruptedException { try { return new Fiber<V>(scheduler, target).start().get(); } catch (ExecutionException e) { throw Exceptions.rethrow(e.getCause()); } }
/** * Runs an action in a new fiber, awaits the fiber's termination, and returns its result. * Unlike {@link #runInFiber(FiberScheduler, SuspendableCallable) runInFiber} this method does not throw {@link ExecutionException}, but wraps * any checked exception thrown by the operation in a {@link RuntimeException}, unless it is of the given {@code exception type}, in * which case the checked exception is thrown as-is. * * @param <V> * @param scheduler the {@link FiberScheduler} to use when scheduling the fiber. * @param target the operation * @param exceptionType a checked exception type that will not be wrapped if thrown by the operation, but thrown as-is. * @return the operations return value * @throws InterruptedException */ public static <V, X extends Exception> V runInFiberChecked(FiberScheduler scheduler, SuspendableCallable<V> target, Class<X> exceptionType) throws X, InterruptedException { try { return new Fiber<V>(scheduler, target).start().get(); } catch (ExecutionException ex) { throw throwChecked(ex, exceptionType); } }
@Override public CloseableHttpResponse execute(final long nanoTime, final HttpRequestBase request) throws SuspendExecution, InterruptedException { // TODO See if timeout can be configured per-request final CloseableHttpResponse ret; try { ret = new Fiber<>((SuspendableCallable<CloseableHttpResponse>) () -> { try { return client.execute(request); } catch (final IOException e) { throw Exceptions.rethrowUnwrap(e); } }).start().get(); } catch (final ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } if (validator != null) { validator.validate(ret); } return ret; }