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 ;
}
这段代码 是我在网上找的可以控制等待时间运行方法的代码 可如果要运行的方法有返回值的话 我该怎么接收呢