Skip to content

Latest commit

 

History

History
executable file
·
50 lines (35 loc) · 1.62 KB

多线程减少上下文切换实战.md

File metadata and controls

executable file
·
50 lines (35 loc) · 1.62 KB

多线程减少上下文切换实战

打印线程信息

jstack0命令dump线程信息,查看pid31177的进程里的线程状态

sudo -u admin /opt/ifeve/java/bin/jstack 31177 > /home/syske/dump17

统计线程状态

统计所有线程分别处于什么状态

grep java.lang.Thread.State dump17 | awk '{pring $2$3$4$5}' | sort | uniq -c

关于线程的状态有一下几种:

  1. 死锁,Deadlock(重点关注)
  2. 执行中,Runnable
  3. 等待资源,Waiting on condition(重点关注)
  4. 等待获取监视器,Waiting on monitor entry(重点关注)
  5. 暂停,Suspended
  6. 对象等待中,Object.wait()TIMED_WAITING
  7. 阻塞,Blocked(重点关注)
  8. 停止,Parked

查看线程详情

打开dump文件查看处于WAITNG(ononjectmonitor)的线程在做什么。

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000001f196719000 nid=0x108 in Object.wait() [0x000000b1981fe000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000000e24ef7b0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
	- locked <0x00000000e24ef7b0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

优化线程池

根据统计及线程详情信息,调整线程池大小,优化WAITING(onobjectmonitor)的线程数量