一阵风° 2023-02-07 22:55 采纳率: 50%
浏览 67
已结题

超时跳出方法代码的返回值问题

public static Object callMethod(final Object target , final String methodName ,final Class<?>[] parameterTypes,final Object[]params,int times){
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
        public String call() throws Exception {
            String value = null  ;
            try {
                Method method = null ;
                method = target.getClass().getDeclaredMethod(methodName , parameterTypes ) ;
                Object returnValue = method.invoke(target, params) ;
                value = returnValue != null ? returnValue.toString() : null ;
            } catch (Exception e) {
                throw e ;
            }
            return value ;
        }
    });
    executorService.execute(future);
    String result = null;
    try{
        result = future.get(times , TimeUnit.SECONDS );
    }catch (Exception e){
        System.out.println("当前终端无法连接");
        e.printStackTrace();
    }
    executorService.shutdownNow();
    return result ;
}

这段代码 是我在网上找的可以控制等待时间运行方法的代码 可如果要运行的方法有返回值的话 我该怎么接收呢

  • 写回答

5条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已结题 (查看结题原因) 2月9日
      • 已采纳回答 2月8日
      • 创建了问题 2月7日

      悬赏问题

      • ¥50 需要加权最小二乘Python代码
      • ¥15 hanzi-writer生成的svg外有一层div,修改div宽高影响的是画布大小,字体在不同视口下完全不相同。我想要让字体跟画布同比例大小该怎么实现?
      • ¥15 Java集合多对一问题集
      • ¥20 PowerShell如何操作记事本?
      • ¥15 intel网卡固件降级失败
      • ¥20 html使用模型的问题
      • ¥15 这些怎么解题啊来个人
      • ¥15 h5套壳IOS Cordova依赖找不到导致编译失败
      • ¥15 分组密码算法misty1的子密钥生成算法c++实现
      • ¥15 vscode的一些报错,希望可以得到解决