Skip to content

Instantly share code, notes, and snippets.

@mszajna
Created July 17, 2023 15:23
Show Gist options
  • Save mszajna/905d122bc4a87056a4ab415ac83ede9f to your computer and use it in GitHub Desktop.
Save mszajna/905d122bc4a87056a4ab415ac83ede9f to your computer and use it in GitHub Desktop.
Apache httpclient 4.x deadlocking with virtual threads
import java.util.concurrent.Executors;
import java.io.IOException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Deadlock {
public static void deadlock(int n) throws IOException {
var request = new HttpGet("https://google.com");
try (var client = HttpClients.createDefault();
var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < n; i++) {
executor.submit(() -> {
System.out.println("Starting job");
try (var response = client.execute(request)) {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {}
System.out.println("Finished job ");
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment