在某些情况下需要控制方法的运行时间,通过Thread+Callable+FutureTask完成,Thread用于新开线程运行指定方法,Callable和FutrueTask用于监控方法运行时间。
详细看代码
package com.budingge.like;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
/**
* java方法运行超时控制
* #在某些情况下需要限制运行时间,超过既定时间久终止运行。
* @author budingge.com
*
*/
public class RuntimeOutControl {
//被测试方法
public void task() throws InterruptedException {
//睡眠1s
Thread.sleep(1000);
for (int i = 0; i < 1000; i++) {
System.out.println("no.:" + i);
}
System.out.println("finished:test");
}
//测试
public void test() throws Exception {
//反射区方法
Method method = getClass().getDeclaredMethod("task", null);
//创建线程实例
Callable call = new CallableTask(this, method, null);
//使用FutureTask具备返回值的线程监控类
FutureTask task = new FutureTask(call);
//创建Thread,用于运行task
Thread thead = new Thread(task);
//设置为后台线程
thead.setDaemon(true);
//启动线程
thead.start();
try {
//设置timeout时间,查看返回结果
task.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println(task);
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
RuntimeOutControl roc = new RuntimeOutControl();
roc.test();
}
}
/**
* 实现Callable接口,具备线程能力,用于执行被监控方法
*
* @author budingge.com
*
*/
class CallableTask implements Callable {
Object cls;// 类实例
Method method;// 要执行类方法
List args;// 参数
/**
* 构造器
*
* @param cls
* @param method
* @param args
*/
public CallableTask(Object cls, Method method, List args) {
super();
this.cls = cls;
this.method = method;
this.args = args;
}
@Override
public Object call() throws Exception {
Object rs = null;
// 反射调用方法
if (args == null) {
rs = method.invoke(cls);
} else {
rs = method.invoke(cls, args.toArray());
}
return rs;
}
}
可能面临几个问题,
1.线程问题,如果方法必需在原来线程中执行,那该如何。
2.性能问题,新开了两个线程来处理,运行方法一个,监控时间一个(猜测)。
3.侵入性太强,耦合度高,如果想使用,必需修改源代码实现。
Hi,2014