-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathConcurrentHttpVirtual.java
More file actions
executable file
·34 lines (32 loc) · 1.09 KB
/
ConcurrentHttpVirtual.java
File metadata and controls
executable file
·34 lines (32 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.net.*;
import java.net.http.*;
import java.util.*;
import java.util.concurrent.*;
/// Proof: concurrent-http-virtual
/// Source: content/concurrency/concurrent-http-virtual.yaml
HttpRequest req(String url) throws Exception {
return HttpRequest.newBuilder(new URI(url)).build();
}
void main() throws Exception {
var client = HttpClient.newHttpClient();
var urls = List.<String>of(); // empty — proof is compile-only
try (var exec = Executors
.newVirtualThreadPerTaskExecutor()) {
var results = urls.stream()
.map(u -> exec.submit(() -> {
try {
return client.send(req(u),
HttpResponse.BodyHandlers.ofString()).body();
} catch (Exception e) {
throw new RuntimeException(e);
}
}))
.toList().stream()
.map(f -> {
try { return f.get(); }
catch (Exception e) { throw new RuntimeException(e); }
}).toList();
}
}