Simple timeout in java
What you are looking for can be found here. It may exist a more elegant way to accomplish that, but one possible approach is Option 1 (preferred): 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 … Read more