Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
data = readSocket();
Coro coro = Coro.initSuspended((@Async({@Await(value = "yield")}) ICoroRunnable) () -> {
Coro.get().yield();
});
следующим шагом надо писать свой новый язык JVM, где можно использовать свои собственные ключевые слова async и awaitДля желающих они реализованы в стандартной scala-library, на базе Future (в java8 запилили аналогичный, хотя и более простой, CompletableFuture).
Action1<HttpServer> onNext = httpServer -> {};
Action1<Throwable> onError = httpServer -> {};
Action0 onComplete = () -> {};
Handler<AsyncResult<HttpServer>> handler1 = RxHelper.toFuture(onNext);
Handler<AsyncResult<HttpServer>> handler2 = RxHelper.toFuture(onNext, onError);
Handler<AsyncResult<HttpServer>> handler3 = RxHelper.toFuture(onNext, onError, onComplete);
The non blocking nature of Vert.x leads to asynchronous APIs. Asynchronous APIs can take various forms including callback style, promises or Rx-style. Vert.x uses callback style in most places (although it also supports Rx).
In some cases, programming using asynchronous APIs can be more challenging than using a direct synchronous style, in particular if you have several operations that you want to do in sequence. Also error propagation is often more complex when using asynchronous APIs.
Vertx-sync allows you to work with asynchronous APIs, but using a direct synchronous style that you’re already familiar with.
@Async static native String get(String url) throws IOException;
static void get(final String url, final AsyncCallback<String> callback) {
XMLHttpRequest xhr = XMLHttpRequest.create();
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onComplete(() -> {
if (xhr.getStatus() != 200) {
callback.error(new IOException("Error loading remote resource " + url + ". Status: " +
xhr.getStatus() + " " + xhr.getStatusText()));
return;
}
callback.complete(xhr.getResponseText());
});
xhr.open("get", url);
xhr.send();
}
System.out.println("Fetching data...");
System.out.println(get("http://localhost:8080"));
System.out.println("Complete");
JCoro — асинхронность на сопрограммах в Java