线程转储文件中有什么呢?
包含每一个线程的状态及调用堆栈信息。
1.thread dump
jstack pid
可以输出堆栈信息到控制台,可用重定向命令写入到文件中。
2.分析
结果为普通文本,可以直接阅读。形如:
2016-11-04 23:53:16 Full thread dump OpenJDK 64-Bit Server VM (24.95-b01 mixed mode): "JDWP Transport Listener: dt_socket" daemon prio=10 tid=0x00007f1b280af000 nid=0x10e4 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Attach Listener" daemon prio=10 tid=0x00007f1b20e59800 nid=0x5c2 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "ajp-nio-8009-exec-10" daemon prio=10 tid=0x00007f1b20e58000 nid=0x6fef waiting on condition [0x00007f1b0eae9000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000000df72b840> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
3.线程状态
大致有RUNNABLE,WAITING ,TIMED_WAITING,BLOCKED这四种状态。
看到很多TIMED_WAITING不要担心,我们若用了java.util.concurrent.ThreadPoolExecutor 这个线程池,那么空闲的线程显示的就是TIMED_WAITING状态。因为它内部是BlockingQueue来盛放任务,若为空,get()的时候自然会阻塞。