// Created on savesnippets.com ยท https://savesnippets.com/NbU6loOSbV1ztQ import java.net.http.*; import java.net.URI; import java.util.*; import java.util.concurrent.*; class Demo { void example() throws Exception { // One-off virtual thread Thread.startVirtualThread(() -> System.out.println("from virtual thread")); // Virtual-thread-per-task executor โ€” spawns a new VT per submit() try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { var client = HttpClient.newHttpClient(); var urls = List.of( "https://example.com", "https://example.org", "https://example.net" ); // Fire 10,000 of these โ€” costs almost nothing var futures = urls.stream().map(url -> executor.submit(() -> { var resp = client.send( HttpRequest.newBuilder(URI.create(url)).build(), HttpResponse.BodyHandlers.ofString() ); return url + " โ†’ " + resp.statusCode(); }) ).toList(); for (var f : futures) System.out.println(f.get()); } } }