Skip to content

Instantly share code, notes, and snippets.

@dhinojosa
Last active March 24, 2024 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhinojosa/9d50d9c3829a8787a8c1fd0b3e96bb3f to your computer and use it in GitHub Desktop.
Save dhinojosa/9d50d9c3829a8787a8c1fd0b3e96bb3f to your computer and use it in GitHub Desktop.
Does this deadlock for you?
package com.evolutionnext.demo.virtualthreads;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.*;
public class VirtualThreadsTryWithResources {
public static void main(String[] args) {
try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
Future<Long> submit = executorService.submit(() -> {
try (InputStream inputStream = new URI("https://openjdk.org/").toURL().openStream();
InputStreamReader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(in)) {
return reader.lines()
.flatMap(line -> Arrays.stream(line.split("\\W+")))
.filter(word -> word.equalsIgnoreCase("java"))
.count();
}
});
System.out.println("Submitted");
System.out.println(submit.get(1, TimeUnit.SECONDS));
} catch (ExecutionException | InterruptedException | TimeoutException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment