您的位置:首页 > 编程语言

利用钩子函数测试某段代码的执行效率

2017-08-19 13:23 239 查看
后期可以利用动态代理切入,或者aop切入

代码如下:

package com.callback;
/**
* main方法测试
* @author tiger
*
*/
public class CountTime {
public static void main(String[] args) {
Test test = new Test();
test.getTime();
}
}
/**
* 测试类
* @author tiger
*
*/
class Test extends GetTime{
@Override
public void userCode() {
for (int i = 0; i < 12000; i++) {
int a = i+i;
System.out.println(a);
//...code....
}
}
}
/**
* 运行时间封装抽象类
* @author tiger
*
*/
abstract class GetTime{
public final void getTime(){
long start=System.currentTimeMillis();	//1s=1000ms

userCode();//钩子、bushi回调

long stop=System.currentTimeMillis();
System.out.println("本程序共花费了:"+(stop-start)+"毫秒");
}
public abstract void userCode();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: