Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/ await in JavaScript and go in Golang #513

Open
oldshensheep opened this issue Oct 5, 2023 · 2 comments
Open

async/ await in JavaScript and go in Golang #513

oldshensheep opened this issue Oct 5, 2023 · 2 comments

Comments

@oldshensheep
Copy link

I have seen this issue #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.

@magicprinc
Copy link

magicprinc commented Nov 24, 2023

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 :-)

@siy
Copy link

siy commented Dec 30, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants