示例代码,10后抛出超时错误,并且取消子线程任务的执行
1 2 3 4 5 6 7 8 9 10 11 12 13
| ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(() -> { .... } );
try { return future.get(10, TimeUnit.SECONDS); } catch (Exception e) { future.cancel(true); executorService.shutdown(); return new ArrayList<>(); }
|