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