@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS).handler(msg -> msg.reply("pong")); // This runs on an event loop but the event loop is at no time blocked! for (int i = 0; i < 10; i++) { System.out.println("Thread is " + Thread.currentThread()); Message<String> reply = awaitResult(h -> eb.send(ADDRESS, "ping", h)); System.out.println("got reply: " + reply.body()); // Like Thread.sleep but doesn't block the OS thread Strand.sleep(1000); } }
protected boolean set(V value) { if (done) return false; if (casSetting(0, 1)) { this.value = value; this.done = true; sync.signalAll(); return true; } else return false; }
/** * Convenience method to interrupt current strand. */ static void selfInterrupt() { Strand.currentStrand().interrupt(); }
protected void maybeSetCurrentStrandAsOwner() { if (owner == null) setStrand(Strand.currentStrand()); else sync.verifyOwner(); }
/** * Convenience method to park and then check if interrupted * * @return {@code true} if interrupted */ private boolean parkAndCheckInterrupt() throws SuspendExecution { Strand.park(this); return Strand.interrupted(); }
@Override public void uncaughtException(Thread t, Throwable e) { uncaughtExceptionHandler.uncaughtException(Strand.of(t), e); } });
public void signalAndWait() throws SuspendExecution { final Strand s = waiter; if (s != null) { record("signal", "%s signalling %s", this, s); Strand.yieldAndUnpark(s, owner); } }
public static Strand of(Object owner) { if (owner instanceof Strand) return (Strand) owner; // if (owner instanceof Fiber) // return (Fiber) owner; else return of((Thread) owner); }
@Override public void run() throws SuspendExecution { try { Val.this.set0(f.run()); } catch (Throwable t) { Val.this.setException0(t); } } }).start();
protected QueueChannel(BasicQueue<Message> queue, OverflowPolicy overflowPolicy, boolean singleProducer, boolean singleConsumer) { this.queue = queue; if (!singleConsumer || queue instanceof CircularBuffer) this.sync = new SimpleConditionSynchronizer(this); else this.sync = new OwnedSynchronizer(this); this.overflowPolicy = overflowPolicy; this.sendersSync = overflowPolicy == OverflowPolicy.BLOCK ? new SimpleConditionSynchronizer(this) : null; this.singleProducer = singleProducer; this.singleConsumer = singleConsumer; }
@Override public Object register() { for(Condition cond : conditions) cond.register(); return null; }
@Override public void unregister(Object registrationToken) { for(Condition cond : conditions) cond.unregister(null); }
@Override public Void run() throws SuspendExecution, InterruptedException { runnable.run(); return null; } }
/** * A utility method that converts a {@link SuspendableRunnable} to a {@link Runnable} so that it could run * as the target of a thread. */ public static Runnable toRunnable(final SuspendableRunnable runnable) { return new SuspendableRunnableRunnable(runnable); }
Channel(Object owner, SingleConsumerQueue<Message, ?> queue) { this.queue = (SingleConsumerQueue<Message, Object>) queue; this.owner = owner; this.sync = OwnedSynchronizer.create(owner); }
/** * Convenience method to interrupt current strand. */ static void selfInterrupt() { Strand.currentStrand().interrupt(); }
/** * Convenience method to park and then check if interrupted * * @return {@code true} if interrupted */ private final boolean parkAndCheckInterrupt() throws SuspendExecution { Strand.park(this); return Strand.interrupted(); }
protected boolean setException(Throwable exception) { if (done) return false; if (casSetting(0, 1)) { this.exception = exception; this.done = true; sync.signalAll(); return true; } else return false; }