final IntChannel fiber2ToFiber1 = Channels.newIntChannel(0); // Synchronizing channel (buffer = 0) Fiber<Integer> fiber1 = new Fiber<>("Fiber1", new SuspendableCallable<Integer>() { @Override public Integer run() throws SuspendExecution, InterruptedException { Fiber<Void> fiber2 = new Fiber<Void>("Fiber2", new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { Integer i; i = fiber1ToFiber2.receive();
/** * Creates a new Fiber subclassing the Fiber class and overriding the {@link Fiber#run() run} method. * * @param name The name of the fiber (may be null) * @param scheduler The scheduler pool in which the fiber should run. * @param stackSize the initial size of the data stack. * @throws NullPointerException when proto is null * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(String name, FiberScheduler scheduler, int stackSize) { return new Fiber<>(name, scheduler, stackSize); }
/** * Creates a new child Fiber from the given {@link SuspendableCallable}. * This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. * The new fiber has no name, and uses the default initial stack size. * * @param target the SuspendableRunnable for the Fiber. * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(SuspendableCallable<V> target) { return new Fiber<>(target); }
/** * Creates a new Fiber subclassing the Fiber class and overriding the {@link Fiber#run() run} method. * The new fiber has no name, and uses the default initial stack size. * * @param scheduler The scheduler pool in which the fiber should run. * @throws NullPointerException when proto is null * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(FiberScheduler scheduler) { return new Fiber<>(scheduler); }
/** * Creates a new child Fiber from the given {@link SuspendableCallable}. * This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. * The new fiber uses the default initial stack size. * * @param name The name of the fiber (may be null) * @param target the SuspendableRunnable for the Fiber. * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(String name, SuspendableCallable<V> target) { return new Fiber<>(name, target); }
/** * Creates a new Fiber from the given {@link SuspendableCallable}. * The new fiber has no name, and uses the default initial stack size. * * @param scheduler The scheduler pool in which the fiber should run. * @param target the SuspendableRunnable for the Fiber. * @throws NullPointerException when proto is null * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(FiberScheduler scheduler, SuspendableCallable<V> target) { return new Fiber<>(scheduler, target); }
@Override public <T> Fiber<T> newFiber(SuspendableCallable<T> target) { return new Fiber<T>(this, target); }
/** * Creates a new Fiber from the given {@link SuspendableCallable}. * The new fiber uses the default initial stack size. * * @param name The name of the fiber (may be null) * @param scheduler The scheduler pool in which the fiber should run. * @param target the SuspendableCallable for the Fiber. * @throws NullPointerException when proto is null * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(String name, FiberScheduler scheduler, SuspendableCallable<V> target) { return new Fiber<>(name, scheduler, target); }
/** * Creates a new child Fiber from the given {@link SuspendableRunnable}. * This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. * The new fiber uses the default initial stack size. * * @param name The name of the fiber (may be null) * @param target the SuspendableRunnable for the Fiber. * @throws NullPointerException when proto is null * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(String name, SuspendableRunnable target) { return new Fiber<>(name, target); }
/** * Creates a new child Fiber from the given {@link SuspendableRunnable}. * This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. * * @param name The name of the fiber (may be null) * @param stackSize the initial size of the data stack. * @param target the SuspendableRunnable for the Fiber. * @throws NullPointerException when proto is null * @throws IllegalArgumentException when stackSize is <= 0 */ public static <V> Fiber<V> create(String name, int stackSize, SuspendableRunnable target) { return new Fiber<>(name, stackSize, target); }
private Strand createStrandForActor(Strand oldStrand, Actor actor) { final Strand strand; if (oldStrand != null) strand = Strand.clone(oldStrand, actor); else strand = new Fiber(actor); actor.setStrand(strand); return strand; }
@Override public void start(Future<Void> startFuture) throws Exception { instanceScheduler = Sync.getContextScheduler(); new Fiber<Void>(instanceScheduler, () -> { try { SyncVerticle.this.start(); startFuture.complete(); } catch (Throwable t) { startFuture.fail(t); } }).start(); }
/** * 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(); }
/** * Runs an action in a new fiber and awaits the fiber's termination. * * @param scheduler the {@link FiberScheduler} to use when scheduling the fiber. * @param target the operation * @throws ExecutionException * @throws InterruptedException */ public static void runInFiber(FiberScheduler scheduler, SuspendableRunnable target) throws ExecutionException, InterruptedException { new Fiber<Void>(scheduler, target).start().join(); }
@Override public void stop(Future<Void> stopFuture) throws Exception { new Fiber<Void>(instanceScheduler, () -> { try { SyncVerticle.this.stop(); stopFuture.complete(); } catch (Throwable t) { stopFuture.fail(t); } finally { Sync.removeContextScheduler(); } }).start(); }
/** * Starts a new fiber and runs the actor in it. * The fiber's name will be set to this actor's name. * * @return This actors' ActorRef */ public ActorRef<Message> spawn() { checkReplacement(); final Fiber f = getName() != null ? new Fiber(getName(), runner) : new Fiber(runner); f.start(); return ref(); }
private static class RequestExecOutcome<Res> { final long execTime; final Res response; final Exception exception; public RequestExecOutcome(final long execTime, final Res response, final Exception exception) { this.execTime = execTime; this.response = response; this.exception = exception; } }
public static Strand clone(Strand strand, final SuspendableCallable<?> target) { if (strand.isAlive()) throw new IllegalStateException("A strand can only be cloned after death. " + strand + " isn't dead."); if (strand instanceof FiberStrand) return clone((Fiber) strand.getUnderlying(), target); if (strand instanceof Fiber) return new Fiber((Fiber) strand, target); else return ThreadStrand.get(cloneThread((Thread) strand.getUnderlying(), toRunnable(target))); }
public static Strand clone(Strand strand, final SuspendableRunnable target) { if (strand.isAlive()) throw new IllegalStateException("A strand can only be cloned after death. " + strand + " isn't dead."); if (strand instanceof FiberStrand) return clone((Fiber) strand.getUnderlying(), target); if (strand instanceof Fiber) return new Fiber((Fiber) strand, target); else return ThreadStrand.get(cloneThread((Thread) strand.getUnderlying(), toRunnable(target))); }
private void requests(Channel<Integer> requestCh, int count) { new Fiber<Void>(() -> { for (int i = 0; i < count; ++i) { requestCh.send(i); } requestCh.close(); }).start(); }