java函数执行超时强制返回
来源:互联网 发布:数据库英文缩写 编辑:程序博客网 时间:2024/06/04 00:31
import java.lang.reflect.Method;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.FutureTask;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;/** * @author Gergul 函数执行超时 * @Notice: 不能执行同名而且参数数量相同的函数!! * @Usage: RunningTimeout.Run(类对象, 方法名字, 参数列表, 超时时间 单位毫秒); */public class RunningTimeout implements Callable<Object>{private Object ower = null;private String sMethodName = null;// private Class[] paramTypes;private Object[] paramValues;// 外部接口public static Object Run(Object ower, String sMethodName,Object[] paramValues, long timeout) throws InterruptedException,ExecutionException, TimeoutException{RunningTimeout rt = new RunningTimeout();return rt._run(ower, sMethodName, paramValues, timeout);}// 不允许newprivate RunningTimeout(){}//private Object _run(Object ower, String sMethodName, Object[] paramValues,long timeout) throws InterruptedException, ExecutionException,TimeoutException{this.ower = ower;this.sMethodName = sMethodName;this.paramValues = paramValues;// 关键代码ExecutorService executor = Executors.newSingleThreadExecutor();FutureTask<Object> future = (FutureTask<Object>)executor.submit(this);executor.execute(future);return future.get(timeout, TimeUnit.MILLISECONDS);}// 真正做事的地方@Overridepublic Object call() throws Exception{// TODO Auto-generated method stubMethod[] methods = ower.getClass().getDeclaredMethods();int n = methods.length;for (int i = 0; i < n; i++){if (sMethodName.equals(methods[i].getName())){Method thisMethod = methods[i];if (thisMethod.getParameterTypes().length == paramValues.length){// 检查参数的数量是否相同return methods[i].invoke(ower, paramValues);}}}throw new Exception("No this method.");}}
0 0
- java函数执行超时强制返回
- 监控函数的执行时间,若超时则强制返回一个默认值
- 为PHP函数执行设置超时
- java多线程实现及超时返回
- 执行、获取远程代码返回:file_get_contents 超时处理
- java 超时任务---设定任务执行时长,超时停止
- Java执行Shell脚本超时控制
- java设置一段代码执行超时时间
- java通过线程控制程序执行超时_hanCSDN_20130815
- java设置一段代码执行超时时间
- Java 得到 执行方法 是否超时
- java中判断线程执行是否超时
- java设置一段代码执行超时时间
- java设置一段代码执行超时时间
- java设置一段代码执行超时时间
- 保持空杯,无惧未来-------sql raiserror() 强制返回 ;sql 调优 ---top 超时
- C#如何控制方法的执行时间,超时则强制退出方法执行
- PHP执行函数超时,执行数据库超时等问题解决方法(9元包邮)
- 黑马程序员----抽象类
- 黑马程序员 java基础学习笔记3
- C51库函数原型列表
- mysql 严重锁表解决方案
- Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)
- java函数执行超时强制返回
- hadoop集群基本配置(大数据学习跟进)
- 关于Python中的for循环控制语句
- 浮点数的比较
- FileLock
- ios 1 cocoa
- 当当结盟一号店
- 修改SVN中文件位置或文件名
- 【已放弃】Pomelo学习笔记(1):安装与配置