|
|
|
|
|
by ffsm8
695 days ago
|
|
I don't think that this would be a good showcase for Virtual Threads.
The "async" API for Java is CompletableFutures, right? thats been stable for something like 10 years, so no real change since Java 8. You'd jsut have to define a ThreadPool with n Threads before, where each request would've blocked one pending thread. Now it just keeps going. So your equivalent Java example should've been something like this, but again: the completeable futures api is pretty old at this point. @HttpExchange(value = "https://news.ycombinator.com")
interface HnClient {
@GetExchange("news?p={page}")
CompletableFuture<String> getNews(@PathVariable("page") Integer page);
}
@RequiredArgsConstructor
@Service
class HnService {
private final HnClient hnClient;
List<String> getNews() {
var requests = IntStream.rangeClosed(1, 4)
.boxed().map(hnClient::getNews).toList();
return requests.stream().map(CompletableFuture::join).toList();
}
}
|
|
Also, I wouldnt consider that the equivalent Java code. That is all Spring and Lombok magic. Just write the code and just use java.net.HttpClient.