public static void main(final String[] args) throws Exception {
        try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
            final HttpGet httpget = new HttpGet("http://httpbin.org/get");

            final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1,
                Log4jThreadFactory.createDaemonThreadFactory("demo"));
            // Cancel the request after once second
            executorService.schedule(httpget::cancel, 1, TimeUnit.SECONDS);

            System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
            httpclient.execute(httpget, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpget + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());
                return null;
            });
        }
    }
This example demonstrates how to abort an HTTP method before its normal completion.
httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientAbortMethod.java