Repository navigation
[Q/A][Java SDK (Component)] For asynchronous calls, should I use asyncCall or virtual threads? #16379
Replies: 2 comments 1 reply
|
Here let me try to answer from what I researched on When you use virtual threads, creating a virtual thread (and the executor to manage it) isn't free — it's cheap, way cheaper than a real OS thread, but it's not zero. There's a small bit of memory and bookkeeping overhead per virtual thread. Using the AsyncCall() style which dubbo uses, you are being handed back the thread by the CompletableFuture, and since dubbo has background threads(netty I/O threads) that hands the result once ready, this is completely non-blocking and dubbo was implementing this asynchronous style way before virtual thread. So choose option asyncCall() which was already doing the same thing even before the virtual thread. @polarisGitHub @zrlw @oxsean @RainYuY did I answered well? |
|
Neither option is universally “better”; they represent two different programming models.
CompletableFuture<Result> future =
RpcContext.getServiceContext()
.asyncCall(() -> remoteService.query(req));
return future.thenApply(this::transform);Ideally, keep the chain asynchronous instead of eventually calling Virtual threads are useful when you prefer straightforward synchronous code. In that case, the important part is that the complete request/task is already running on a virtual thread. You can then call the synchronous Dubbo proxy directly: Result result = remoteService.query(req);
return transform(result);Wrapping one synchronous call in a newly created executor and immediately calling try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
Future<A> a = executor.submit(() -> serviceA.query(req));
Future<B> b = executor.submit(() -> serviceB.query(req));
return combine(a.get(), b.get());
}Therefore:
Also be careful with Finally, virtual threads should not be used as a downstream concurrency limit. If the remote service must receive at most N concurrent calls, use a Oracle’s virtual-thread guidance makes the same distinction: synchronous blocking code benefits from virtual threads, while an already non-blocking Dubbo 3.3 also documents Project Loom support: |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Pre-check
Apache Dubbo Component
Java SDK (apache/dubbo)
Details
Which way is better?
Code of Conduct
All reactions