在Arthas官网:https://arthas.aliyun.com/中下载安装包。
执行java -jar arthas-boot.jar就可以启动。



通过thread -b查看阻塞线程信息。


watch:查看方法参数
trace:查看方法内部调用路径,可以看到每条路径的耗时
stack:查看方法调用路径
redefine:将编译好的class热部署到环境
ognl:可以查看类中属性和方法执行情况。

示例代码:
public class Arthas {
private static HashSet hashSet = new HashSet();
public String getName() {
return "arthas";
}
public static void main(String[] args) {
// 模拟 CPU 过高
cpuHigh();
// 模拟线程死锁
deadThread();
// 不断的向 hashSet 集合增加数据
addHashSetThread();
}
/**
* 不断的向 hashSet 集合添加数据
*/
public static void addHashSetThread() {
// 初始化常量
new Thread(() -> {
int count = 0;
while (true) {
try {
hashSet.add("count" + count);
Thread.sleep(1000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"my-arthas-thread").start();
}
public static void cpuHigh() {
new Thread(() -> {
while (true) {
}
},"my-arthas-thread1").start();
}
/**
* 死锁
*/
private static void deadThread() {
/** 创建资源 */
Object resourceA = new Object();
Object resourceB = new Object();
// 创建线程
Thread threadA = new Thread(() -> {
synchronized (resourceA) {
System.out.println(Thread.currentThread() + " get ResourceA");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get resourceB");
synchronized (resourceB) {
System.out.println(Thread.currentThread() + " get resourceB");
}
}
},"my-arthas-thread2");
Thread threadB = new Thread(() -> {
synchronized (resourceB) {
System.out.println(Thread.currentThread() + " get ResourceB");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get resourceA");
synchronized (resourceA) {
System.out.println(Thread.currentThread() + " get resourceA");
}
}
},"my-arthas-thread3");
threadA.start();
threadB.start();
}
}查看死锁:
通过thread -b查看死锁情况:

查看CPU飙升:
通过dashboard就能查看CPU占用情况:

查看GC情况:
通过dashboard就能查看GC情况:

查看接口调用太慢:
通过stack 来查看方法调用次数和时长。


本文由博客一文多发平台 OpenWrite 发布!