Simple timeout in java - Stack Overflow

PHOTO EMBED

Sat May 16 2020 19:40:35 GMT+0000 (UTC)

Saved by @price_the_ice #java

final Duration timeout = Duration.ofSeconds(30);
ExecutorService executor = Executors.newSingleThreadExecutor();

final Future<String> handler = executor.submit(new Callable() {
    @Override
    public String call() throws Exception {
        return requestDataFromModem();
    }
});

try {
    handler.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
    handler.cancel(true);
}

executor.shutdownNow();
content_copyCOPY

https://stackoverflow.com/questions/19456313/simple-timeout-in-java