java方法执行超时关闭

ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Boolean> future = executorService.submit(() -> method(param));
try {
//设置超时时间
boolean rst = future.get(200, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
System.out.println("执行超时!");
} catch(Exception e){
System.out.println("获取数据异常," + e.getMessage());
}finally {
executorService.shutdown();
}

  

posted @ 2021-05-17 19:33  MartialWorldFish  阅读(542)  评论(0编辑  收藏  举报