(2) 找出佔用cpu最高的線程
top -Hp 1781
(3) 佔CPU最高線程17596換算成16進制對應線程44bc
用命令
printf "%x\n" 17596
(4) 打印佔CPU最高JAVA進程1781的堆棧信息
jstack
1781> stackdump.txt
代碼以下:
public class CPUConsumeTest {
public static void main(String[] args) {
int count = Runtime.getRuntime().availableProcessors();
System.out.println("processors " + count);
for(int i=0; i<count; i++) {
new Thread(new ConsumeCPUTest()).start();
}
for(int i=0; i<100; i++) {
new Thread(new NotConsumeCPUTest()).start();
}
}
}
class ConsumeCPUTest implements Runnable {
public void run() {
while(true) {
for(int i=0; i<10000000; i++) {
long l = 1000000;
Math.acos(l);
}
try {
Thread.currentThread().sleep(20);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
class NotConsumeCPUTest implements Runnable {
public void run() {
while(true) {
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
二、sy太高,上下文切換過頻繁,將線程棧打印出來,看是否有哪一個鎖競爭過於激烈