首页  

找出最耗CPU的java线程和代码     所属分类 java 浏览量 1504
写一个耗cpu的Java代码  模拟一下 cputest.java  代码见最后

编译运行cputest
javac cputest.java
java cputest


找出cputest 对应的进程id 
ps aux|grep cputest
ps aux|grep cputest |grep -v 'grep'
ps aux|grep cputest |grep -v 'grep'|awk '{print $2}'
pid 24271

使用top命令找出最耗 cpu的线程 
top -p 24271 -H

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                      
24272 root      20   0 2461160  25708  11252 S 45.5  1.4  11:17.99 java                                                         
24280 root      20   0 2461160  25708  11252 S  0.3  1.4   0:00.51 java                                                         
24271 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.00 java                                                         
24273 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.02 java                                                         
24274 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.00 java                                                         
24275 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.00 java                                                         
24276 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.00 java                                                         
24277 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.01 java                                                         
24278 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.01 java                                                         
24279 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.00 java                                                         
24370 root      20   0 2461160  25708  11252 S  0.0  1.4   0:00.00 java                                                         

最耗 cpu的线程ID 为 24272
转换成16进制 
printf "%x\n" 24272
5ed0

使用jstack 打印 线程堆栈 
jstack 24271
找到 5ed0 对应的线程

nid=0x5ed0

"main" #1 prio=5 os_prio=0 tid=0x00007fb658009000 nid=0x5ed0 waiting on condition [0x00007fb660ca5000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at cputest.main(cputest.java:31)




cputest.java  

public class cputest {
		
	private static final long MAX_SEQ = 10000000;
	private static final long SLEEP_TIME = 10;
	private static final long SLEEP_TIME_MAX = 1000;

	public static void main(String[] args) throws Exception {
		
		long sleepTime = SLEEP_TIME;
		if(args.length>0){
			try{
			   sleepTime = Long.parseLong(args[0]);
			}catch(Throwable e){
				// do nothing
			}
		}
		if(sleepTime<=2 || sleepTime> SLEEP_TIME_MAX){
			sleepTime = SLEEP_TIME_MAX;
		}
		
		System.out.println("MAX_SEQ="+MAX_SEQ);
		System.out.println("sleepTime="+sleepTime);

		boolean flag = true;
		long seq = 0;
		while(flag){
			seq++;
			if(seq>MAX_SEQ){
				seq = 0;
				Thread.sleep(sleepTime);
			}
		}
	}

}

上一篇     下一篇
linux手工释放cache

如何让pre元素里的内容自动换行

jdk自带工具使用

线上问题排查常用命令

Lock和synchronized的区别

java并发机制的底层实现原理