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();
        }

 

posted @ 2021-06-01 21:58  MartialWorldFish  阅读(326)  评论(0编辑  收藏  举报