@Override protected Integer run() throws SuspendExecution, InterruptedException { System.out.println("Hello world2!"); Fiber.sleep(1000); System.out.println("Hello world3!"); return 2; } }.start();
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();
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(); } }
@Suspendable @Override public void start() throws Exception { // Wait for a single event - this just demonstrates that you can wait for single events System.out.println("Waiting for single event"); long tid = awaitEvent(h -> vertx.setTimer(1000, h)); System.out.println("Single event has fired"); }
@Override @Suspendable public final void join() throws ExecutionException, InterruptedException { get(); }
private static FiberScheduler defaultScheduler() { final Fiber parent = currentFiber(); if (parent == null) return DefaultFiberScheduler.getInstance(); else return parent.getScheduler(); }
@Override public void run(Fiber f) { f.record(1, "Fiber", "parkAndSerialize", "Serializing fiber %s", f); writer.write(f, getFiberSerializer()); } }));
@SuppressWarnings("empty-statement") public V run() throws E, SuspendExecution { final FiberCallback handler = new FiberCallback(); while(!Fiber.park(this, handler)) // make sure we actually park and run PostParkActions ; while (!handler.isCompleted()) Fiber.park(this); return handler.getResult(); }
final boolean exec(Object blocker, long timeout, TimeUnit unit) { if (!scheduler.isCurrentThreadInScheduler()) return false; record(1, "Fiber", "exec", "Blocker %s attempting to immediately execute %s", blocker, this); if (!tryUnpark(blocker, timeout, unit)) { record(1, "Fiber", "exec", "Blocker %s attempt to immediately execute %s FAILED", blocker, this); return false; } immediateExecHelper(); return true; }
@Override public final String getName() { if (name == null) // benign race this.name = "fiber-" + ((scheduler != null && scheduler != DefaultFiberScheduler.getInstance()) ? (scheduler.getName() + '-') : "") + fid; return name; }
private boolean tryUnpark(Object unblocker, long timeout, TimeUnit unit) { long start = 0; for (int i = 0;; i++) { Object b = getBlocker(); boolean tu; if ((unblocker != null ? b == unblocker : true) && (tu = task.tryUnpark(unblocker))) return true; if ((start = isTimeoutExpired(i, start, timeout, unit)) < 0) return false; } }
@Override @Suspendable public void start() throws Exception {
@Override public Integer run() throws SuspendExecution, InterruptedException { Fiber.sleep(1000); fiber1ToFiber2.send(1); Integer i1 = fiber2ToFiber1.receive(); System.out.println(" Hello words " + i1); fiber1ToFiber2.send(9); Integer i2 = fiber2ToFiber1.receive(); System.out.println(" Hello words " + i2); fiber1ToFiber2.send(0); return i2; } }).start();
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; } }
@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); } }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS).handler(msg -> { System.out.println("Waiting"); // reply after one second vertx.setTimer(1000, tid -> msg.reply("wibble")); }); // If you want to do sync stuff in an async handler it must be transformed to a fiber handler vertx.createHttpServer().requestHandler(fiberHandler(req -> { // Send a message to address and wait for a reply Message<String> reply = awaitResult(h -> eb.send(ADDRESS, "blah", h)); System.out.println("Got reply: " + reply.body()); req.response().end("blah"); })).listen(8080, "localhost"); }
@Override @Suspendable public void start() throws Exception { JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true") .put("driver_class", "org.hsqldb.jdbcDriver"); JDBCClient jdbc = JDBCClient.createShared(vertx, config); // Get a connection try (SQLConnection conn = awaitResult(jdbc::getConnection)) { // Create a table Void v = awaitResult(h -> conn.execute("CREATE TABLE test(col VARCHAR(20))", h)); // Insert some stuff for (int i = 0; i < 10; i++) { int ii = i; UpdateResult res = awaitResult(h -> conn.update("INSERT INTO test (col) VALUES ('val" + ii + "')", h)); System.out.println("Rows updated: " + res.getUpdated()); } // Select the results ResultSet res = awaitResult(h -> conn.query("SELECT * FROM test", h)); System.out.println("Selected " + res.getNumRows() + " results"); res.getResults().forEach(System.out::println); } }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Create a couple of consumers on different addresses // The adaptor allows handler to be used as a Channel HandlerReceiverAdaptor<Message<String>> adaptor1 = streamAdaptor(); eb.<String>consumer(ADDRESS1).handler(adaptor1); HandlerReceiverAdaptor<Message<String>> adaptor2 = streamAdaptor(); eb.<String>consumer(ADDRESS2).handler(adaptor2); // Set up a periodic timer to send messages to these addresses vertx.setPeriodic(500, tid -> { eb.send(ADDRESS1, "wibble"); eb.send(ADDRESS2, "flibble"); }); // 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> received1 = adaptor1.receive(); System.out.println("got message: " + received1.body()); Message<String> received2 = adaptor2.receive(); System.out.println("got message: " + received2.body()); } }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Create a couple of consumers on different addresses // The adaptor allows handler to be used as a Channel HandlerReceiverAdaptor<Message<String>> adaptor1 = streamAdaptor(); eb.<String>consumer(ADDRESS1).handler(adaptor1); HandlerReceiverAdaptor<Message<String>> adaptor2 = streamAdaptor(); eb.<String>consumer(ADDRESS2).handler(adaptor2); // Set up a periodic timer to send messages to these addresses vertx.setPeriodic(500, tid -> { eb.send(ADDRESS1, "wibble"); eb.send(ADDRESS2, "flibble"); }); ReceivePort<Message<String>> channel1 = adaptor1.receivePort(); ReceivePort<Message<String>> channel2 = adaptor2.receivePort(); // Combine them into a single channel // Not sure how to avoid this ugly cast with Quasar Mix<Message<String>> mix = (Mix<Message<String>>)Channels.mix(channel1, channel2); // Take the first ten for (int i = 0; i < 10; i++) { Message<String> msg = mix.receive(); System.out.println("got message: " + msg.body()); } System.out.println("done"); }