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 { return i2; }).start(); Fiber<Void> fiber2 = new Fiber<Void>("Fiber2", new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { Integer i; i = fiber1ToFiber2.receive(); i = fiber1ToFiber2.receive(); } }).start(); fiber1.join(); fiber2.join(); return fiber1.get();
@Override protected Integer run() throws SuspendExecution, InterruptedException { System.out.println("Hello world2!"); Fiber.sleep(1000); System.out.println("Hello world3!"); return 2; } }.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(); }
@Override public Strand start() { fiber.start(); return this; }
/** * 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); }
/** * 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(); }
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 void parkNanos(Object blocker, long nanos) throws SuspendExecution, InterruptedException { if (Fiber.currentFiber() != null) Fiber.park(blocker, nanos, TimeUnit.NANOSECONDS); else LockSupport.parkNanos(blocker, nanos); }
/** * * @param immediateExec Whether the fiber should be executed in the same thread as the callback. Should generally be set to `false`. */ public FiberAsync(boolean immediateExec) { this.fiber = Fiber.currentFiber(); this.immediateExec = immediateExec; }
public static void sleep(long millis) throws SuspendExecution, InterruptedException { if (Fiber.currentFiber() != null) Fiber.sleep(millis); else Thread.sleep(millis); }
@Override public T get() { final Thread thread = Thread.currentThread(); final Fiber fiber = Fiber.currentFiber(); if (fiber != null) fiber.restoreThreadLocals(thread); try { return super.get(); } finally { if (fiber != null) fiber.installFiberLocals(thread); } }
/** * Deserializes a fiber from the given byte array and unparks it. * * @param serFiber The byte array containing a fiber's serialized form. * @param scheduler The {@link FiberScheduler} to use for scheduling the fiber. * @return The deserialized, running fiber. */ public static <V> Fiber<V> unparkSerialized(byte[] serFiber, FiberScheduler scheduler) { final Fiber<V> f = (Fiber<V>) getFiberSerializer().read(serFiber); return unparkDeserialized(f, scheduler); }
private static FiberScheduler currentScheduler() { final Fiber currentFiber = Fiber.currentFiber(); if (currentFiber == null) throw new IllegalStateException("Method called not within a fiber"); return currentFiber.getScheduler(); }
@Override public void handleInterruptedException(InterruptedException e) { Fiber.currentFiber().interrupt(); }
public static boolean interrupted() { if (Fiber.currentFiber() != null) return Fiber.interrupted(); else return Thread.interrupted(); }
public static void yield() throws SuspendExecution { if (Fiber.currentFiber() != null) Fiber.yield(); else Thread.yield(); }
public static void dumpStack() { if (Fiber.currentFiber() != null) Fiber.dumpStack(); else Thread.dumpStack(); }
public static boolean interrupted() { final Fiber current = currentFiber(); if (current == null) throw new IllegalStateException("Not called on a fiber"); final boolean interrupted = current.isInterrupted(); if (interrupted) current.interrupted = false; return interrupted; }