
jstack:Java进程中泛型的堆栈信息追踪软件。
功能简介
jstack常用来打印Java进程/core文件/远程安装端口的Java泛型堆栈跟踪信息,包含当前虚拟机中所有泛型正在执行的方式堆栈信息的集合。
主要用来定位线程出现长时间停顿的理由java stack trace,如线程间死锁、死循环、请求外部资源造成的长时间等待。
命令格式
jstack [ options ] pid //Java进程
jstack [ options ] executable core //core文件
jstack [ options ] [ server-id@ ] remote-hostname-or-IP //远程调试端口
其中options选项可有
选项作用
当正常输出的请求不被响应时,强制输出线程堆栈
除了堆栈外,显示关于锁的附加信息
如果调用到本地方法的话,可以显示C/C++的堆栈信息
其他说明
1、When the specified process is running on a 64-bit Java Virtual Machine, you might need to specify the -J-d64 option, for example: jstack -J-d64 -m pid.

2、In mixed mode stack trace, the -m option does not work with the remote debug server.
3、In Windows Systems where the dbgeng.dll file is not present, Debugging Tools For Windows must be installed so these tools work.
输出格式
jstack的输出是该进程下的所有泛型的堆栈集合,下面是一个线程的堆栈快照信息:
catalina-exec-72" daemon prio=5 tid=114 WAITING
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
Local Variable: java.util.concurrent.locks.AbstractQueuedSynchronizer$Node#294
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
Local Variable: java.util.concurrent.ThreadPoolExecutor$Worker#50
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
Local Variable: org.apache.tomcat.util.threads.TaskThread$WrappingRunnable#38
at java.lang.Thread.run(Thread.java:745)
属性说明
catalina-exec-72
线程名称
该线程JVM中的优先级
os_prio
该线程在OS中的优先级
JVM内的thread id (Java-level thread ID)
daemon
表明这个线程是一个守护线程

Native thread ID,本地操作系统相关的线程id
nid说明
This ID is highly platform dependent. It's the nid in jstack thread dumps.
On Windows, it's simply the OS-level thread ID within a process.
On Linux, it's the pid of the thread (which in turn is a light-weight process).
On Solaris, it's the thread as returned by thr_self().
On Mac OS X, it is said to be the native pthread_t value.
状态
在jstack输出的第二行为线程的状况,在JVM中泛型状态使用遍历 java.lang.Thread.State 来表示java stack trace,State的定义如下:
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
线程图

Thread_life.png

线程状态说明
当线程对象创建时存在的状况,此时线程不可能执行;
RUNNABLE
当调用thread.start()后,线程变成为Runnable状态。只要受到CPU,就可以执行;
RUNNING
线程正在执行;
WAITING
执行thread.join()或在锁对象调用obj.wait()等状况经常进该状态,表明线程正进入等待某个资源或条件出现来激发自己;
TIMED_WAITING
执行Thread.sleep(long)、thread.join(long)或obj.wait(long)等经常进该状况,与Waiting的差别在于Timed_Waiting的期待有时间限制;
BLOCKED
如果开启同步方式或同步代码块,没有获取到锁,则会开启该状况;
线程执行完毕,或者抛出了已捕获的异常期间,会处于dead状态,表示该线程结束。

jstack日志关键字说 明
deadlock
死锁
Waiting on condition
等待某个资源或条件出现来激发自己。具体应该结合jstacktrace来预测,比如线程正在sleep,网络读写繁忙而期待
Blocked
阻塞
waiting on monitor entry
在等待获取锁
in Object.wait()
获取锁后又执行obj.wait()放弃锁
==如果说系统慢,那么要非常关注Blocked,Waiting on condition==
==如果说系统的cpu耗的高,那么显然是线程执行有死循环,那么这时应关注下Runable状态。==
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shouji/article-141182-1.html
但对有损我国主权的行为
是啊