btrace 官网地址(已关闭):https://kenai.com/projects/btrace
实现原理参考:http://www.ibm.com/developerworks/cn/java/j-lo-instrumentation/
1、首先下载visualvm,并安装btrace插件
2、在eclipse编写测试类代码:
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BTraceTest {
public int add(int a, int b) {
return a + b;
}
public void run(){
int a = (int) (Math.random() * 1000);
int b = (int) (Math.random() * 1000);
System.out.println(add(a, b));
}
public static void main(String[] args) throws IOException {
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
BTraceTest bTraceTest=new BTraceTest();
bReader.readLine();
for (int i = 0; i < 10; i++) {
bTraceTest.run();
}
}
}
运行Main函数,阻塞等待控制台输入。
2、打开visualvm,会自动检测到本地的java程序,右键》trace application
3、输入下面红框中的代码:
4、点击start开始编译
5、回到Eclipse 在控制台回车,让程序继续执行,完成后回到VisualVM 则看到控制台输出如下信息