manifold
manifold copied to clipboard
async/ await in JavaScript and go in Golang
I have seen this issue https://github.com/manifold-systems/manifold/issues/147 , but currently, there are no additional keywords for virtual threads like async/await or go.
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder().GET().uri(URI.create("https://example.com")).build();
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
response.thenAccept(s -> System.out.println(s.body()));
client.close();
with async like this
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder().GET().uri(URI.create("https://example.com")).build();
HttpResponse<String> response = await client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body())
client.close();
like golang go
, we can have jo
var list = new ArrayList<Integer>();
jo {
for (int i = 0; i < 1 << 10; i++) {
list.add(i);
}
}
var list = new ArrayList<Integer>();
Executors.newVirtualThreadPerTaskExecutor().submit(() -> {
for (int i = 0; i < 1 << 10; i++) {
list.add(i);
}
});
I think the keyword jo
implement will be easy? but for async/await we need handle exception, I don't know if it is easy.
var list = new ArrayList<Integer>();
Executors.newVirtualThreadPerTaskExecutor().submit(() -> {
for (int i = 0; i < 1 << 10; i++) {
list.add(i);
}
});
Is this thread safe? (and for tasks without result, execute(runnable)
is better)
var resultList = Executors.newVirtualThreadPerTaskExecutor().submit(() -> {
var list = new ArrayList<Integer>();
for (int i = 0; i < 1 << 10; i++) {
list.add(i);
}
return list;
}).get();
looks safer
With VT, is this await
just a Syntactic sugar for:
@Test void name (){
assertEquals(42, await(()->21 * 2));
}
@SneakyThrows // InterruptedException,CancellationException; any other Exception/Throwable from caller's Callable.call
public static <T> T await (Callable<T> asyncTask){
FutureTask<T> task = new FutureTask<>(asyncTask);
Thread.startVirtualThread(task);
try {
return task.get();
} catch (ExecutionException | CompletionException e){
throw e.getCause() != null ? e.getCause() : e;
}
}
The other question: why use async API in VT-World: use blocking calls in VT
Virtual Threads kill not only reactive but also CompletableFuture :-)
Virtual threads do not kill anything and "synchronous" style has its own set of issues. But reactive and CompletableFuture are not even contenders in regard to simplicity and convenience. Reactive enforces quite artificial processing model with technical details leaking into the business logic. CompletableFuture is no better with complex, mouthful and cumbersome API and misleading documentation. For example, references to similarly of .thenApply to map is false, one can turn successful result into failure by throwing an exception, Worse is that all mentioned above API styles share same painful and prone to mistakes exception-based error management.The async/await does not change anything in this regard. To me it even worse than the "synchronous" style.