4.jstack
jstack用于显示指定进程内线程的信息
语法:
jstack [option] <pid>
说明:
option:命令选项,常用选项如下:
-F当’jstack [-l] pid’没有响应的时候强制打印栈信息,如果直接jstack无响应时,用于强制jstack),一般情况不需要使用
-l长列表. 打印关于锁的附加信息,例如属于java.util.concurrent的ownable synchronizers列表,会使得JVM停顿得长久得多(可能会差很多倍,比如普通的jstack可能几毫秒和一次GC没区别,加了-l 就是近一秒的时间),-l 建议不要用。一般情况不需要使用
-m打印java和native c/c++框架的所有栈信息.可以打印JVM的堆栈,显示上Native的栈帧,一般应用排查不需要使用
我运行如下代码
public class test {
public static void main(String[] args) {
while(true) {
}
}
}
我此时执行 jps命令结果如图
我对于名为test的进程执行 jstack命令 也就是输入 jstack 17760
获得如下结果
2019-07-21 16:32:59
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Service Thread" #7 daemon /*左边此处是表明是否是守护线程*/prio=9 /*此处说明线程的优先级*/ os_prio=0 /*左边此处说明该线程对应的操作系统线程的优先级*/tid=0x158f2400/*此处说明Java中线程编号*/ nid=0x7240/*即native id,该线程对应的操作系统中本地线程编号,每一个java线程都有一个对应的操作系统线程*/ runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x15896400 nid=0x794 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x15895400 nid=0x7420 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x158c3000 nid=0x39b0 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
/*上边这出java.lang.Thread.State是说明线程状态代码下部将会进行解释*/
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x1587a000 nid=0x501c in Object.wait() [0x15aef000]
java.lang.Thread.State: WAITING (on object monitor)/*表示该线程处于等待状态,括号中的内容说明了导致等待的原因。*/
at java.lang.Object.wait(Native Method)
- waiting on <0x05407ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x05407ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15864400 nid=0x4584 in Object.wait() [0x053ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05405f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x05405f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x02cbc800 nid=0x7778 runnable [0x02d0f000]
java.lang.Thread.State: RUNNABLE
at vipstudytest.test.main(test.java:7)
"VM Thread" os_prio=2 tid=0x0323d000 nid=0x7624 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x15946800 nid=0x59a0 waiting on condition
JNI global references: 6
线程与Monitor
|
进入区(Entrt Set):表示线程通过synchronized要求获取对象的锁。如果对象未被锁住,则迚入拥有者;否则则在进入区等待。一旦对象锁被其他线程释放,立即参与竞争。
拥有者(The Owner):表示某一线程成功竞争到对象锁。
等待区(Wait Set) :表示线程通过对象的wait方法,释放对象的锁,并在等待区等待被唤醒。
说明:
一个 Monitor在某个时刻,只能被一个线程拥有,该线程就是 “Active Thread”,而其它线程为“Waiting Thread”,分别在两个队列 “ Entry Set”和 “Wait Set”里面等候。在 “Entry Set”中等待的线程状态是 “Waiting for monitor entry”,而在“Wait Set”中等待的线程状态是 “in Object.wait()”。 被 synchronized保护起来的代码段称为临界区,当一个线程申请进入临界区时,它就进入了 “Entry Set”队列。
线程状态:
1、NEW:线程刚刚被创建,也就是已经new过了,但是还没有调用start()方法,jstack命令不会列出处于此状态的线程信息。
2、RUNNABLE:RUNNABLE这个名字很具有欺骗性,很容易让人误以为处于这个状态的线程正在运行;事实上,这个状态只是表示,线程是可运行的。一个单核CPU在同一时刻,只能运行一个线程。
3、BLOCKED:线程处于阻塞状态,正在等待一个监视器锁(monitor lock)。通常情况下,是因为本线程与其他线程公用了一个锁。其他在线程正在使用这个锁进入某个synchronized同步方法块或者方法,而本线程进入这个同步代码块也需要这个锁,最终导致本线程处于阻塞状态。
例子:
private static Object lock = new Object();
public static void main(String[] args) {
new Thread(new Task(), "线程1").start();
new Thread(new Task(), "线程2").start();
}
static class Task implements Runnable {
@Override
public void run() {
synchronized (lock) {
count();
}
}
private void count() {
while (true) {
}
}
}
得到如下结果:
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"DestroyJavaVM" #10 prio=5 os_prio=0 tid=0x029bc800 nid=0x7728 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"线程2" #9 prio=5 os_prio=0 tid=0x15555400 nid=0x5e00 waiting for monitor entry/*说明线程2处于"Entry Set"队列,等待monitor*/ [0x15aff000]
java.lang.Thread.State: BLOCKED (on object monitor)/*线程2处于BLOCK状态*/
at vipstudytest.test$Task.run(test.java:17)
- waiting to lock <0x050bda18> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:748)
"线程1" #8 prio=5 os_prio=0 tid=0x15554c00 nid=0x50b8 runnable [0x15a6f000]
java.lang.Thread.State: RUNNABLE/*线程1获取到锁,处于RUNNABLE状态*/
at vipstudytest.test$Task.count(test.java:22)
at vipstudytest.test$Task.run(test.java:17)
- locked <0x050bda18> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x154ecc00 nid=0x6c94 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x15496400 nid=0x5bcc waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x15495800 nid=0x76f8 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x154c3000 nid=0x447c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x1547a000 nid=0x32dc in Object.wait() [0x156af000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05007ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x05007ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15464400 nid=0x4490 in Object.wait() [0x04f9f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05005f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x05005f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x02ead000 nid=0x2d4 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x15540c00 nid=0x54dc waiting on condition
4、WAITING:等待状态,等待某个condition或monitor发生,调用以下方法可能会导致一个线程处于等待状态:
wait() 不指定超时时间,例子:
public class Test {
public static void main(String[] args) {
Object lock = new Object();
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode, sharing):
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x021a6c00 nid=0x5b9c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x0219fc00 nid=0x606c waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0219ec00 nid=0x2830 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0219d000 nid=0x719c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x02184000 nid=0x3fcc in Object.wait() [0x1484f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04808978> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x04808978> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x02127400 nid=0x6814 in Object.wait() [0x0277f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04806a90> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x04806a90> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x027fc800 nid=0x767c in Object.wait() [0x0083f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x048bdfc8> (a java.lang.Object)
at java.lang.Object.wait(Object.java:502)
at vipstudytest.Test.main(Test.java:9)
- locked <0x048bdfc8> (a java.lang.Object)
/*1、main线程先locked <0x027fc800>,再waiting on <0x027fc800>,之所以先锁再等同一个对象,是因为wait方法需要先通过synchronized获得该地址对象的monitor;
2、waiting on <0x027fc800>说明线程执行了wait方法之后,释放了monitor,进入到"Wait Set"队列,等待其它线程执行地址为0x027fc800对象的notify方法,并唤醒自己,
*/
"VM Thread" os_prio=2 tid=0x02123800 nid=0x769c runnable
"VM Periodic Task Thread" os_prio=2 tid=0x15c90800 nid=0x6e20 waiting on condition
JNI global references: 6
join() 不指定超时时间,例子:
public class Test {
public static void main(String[] args) {
CountThread countThread = new CountThread();
countThread.start();
try {
countThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class CountThread extends Thread{
@Override
public void run() {
while(true) {
}
}
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Thread-0" #8 prio=5 os_prio=0 tid=0x15b53800 nid=0x3c54 runnable [0x1601f000]
java.lang.Thread.State: RUNNABLE
at vipstudytest.Test$CountThread.run(Test.java:19)
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x15ac9800 nid=0x74c0 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x15a98400 nid=0x75cc waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x15ac3c00 nid=0x7338 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x15ac2c00 nid=0x655c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x15a7a000 nid=0x6e04 in Object.wait() [0x055ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05607ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x05607ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15a64400 nid=0x75d4 in Object.wait() [0x0555f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05605f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x05605f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x034ac800 nid=0x39b0 in Object.wait() [0x0165f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x056bf350> (a vipstudytest.Test$CountThread)
at java.lang.Thread.join(Thread.java:1252)
- locked <0x056bf350> (a vipstudytest.Test$CountThread)
at java.lang.Thread.join(Thread.java:1326)
at vipstudytest.Test.main(Test.java:9)
"VM Thread" os_prio=2 tid=0x02fad000 nid=0x5a58 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x15acd800 nid=0x3640 waiting on condition
JNI global references: 6
park() 例子:程序文件夹
import java.util.concurrent.locks.LockSupport;
public class Test{
public static void main(String[] args) {
LockSupport.park();//禁用执行该行代码的线程。
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x15430800 nid=0x5538 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x153d6400 nid=0x6900 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x153d5800 nid=0x4758 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x15403000 nid=0x5c74 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x153ba000 nid=0x6cfc in Object.wait() [0x156bf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04e07ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x04e07ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x153a4400 nid=0x4a9c in Object.wait() [0x1562f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04e05f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x04e05f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x00e9c800 nid=0x7584 waiting on condition [0x010cf000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304)
at vipstudytest.Test.main(Test.java:8)
"VM Thread" os_prio=2 tid=0x02d3d000 nid=0x7714 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x1548c800 nid=0x74c4 waiting on condition
JNI global references: 6
5、TIMED_WAITING:线程等待指定的时间,对于以下方法的调用,可能会导致线程处于这个状态:
wait(long timeout) 指定超时时间,例子:
public class Test{
public static void main(String[] args) {
Object lock = new Object();
synchronized (lock) {
try {
lock.wait(Long.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x14d02800 nid=0x1450 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x14ccb400 nid=0x1ec4 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x14cca400 nid=0x1fbc waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x14cc7400 nid=0x1fb0 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x14c9f400 nid=0x1ef8 in Object.wait() [0x151ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04807ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x04807ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x14c79400 nid=0x1eec in Object.wait() [0x14f9f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04805f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x04805f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x0020cc00 nid=0x1bc4 in Object.wait() [0x007ff000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)/*此处意思就是等待的时间是有限的*/
at java.lang.Object.wait(Native Method)
- waiting on <0x048ba180> (a java.lang.Object)
at Test.main(Test.java:7)
- locked <0x048ba180> (a java.lang.Object)
"VM Thread" os_prio=2 tid=0x14c73000 nid=0x1a0 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x14cd2800 nid=0x1d50 waiting on condition
JNI global references: 6
join(long millis) 指定超时时间,例子:
public class Test {
public static void main(String[] args) {
CountThread countThread = new CountThread();
countThread.start();
try {
countThread.join(Long.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class CountThread extends Thread{
@Override
public void run() {
while(true) {
}
}
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Thread-0" #8 prio=5 os_prio=0 tid=0x15353800 nid=0x627c runnable [0x1593f000]
java.lang.Thread.State: RUNNABLE
at vipstudytest.Test$CountThread.run(Test.java:19)
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x152eb000 nid=0x1acc runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x152c4000 nid=0x6250 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x15297800 nid=0x7104 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x152c3000 nid=0x1d6c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x1527a000 nid=0xd1c in Object.wait() [0x1557f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04e07ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x04e07ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15264400 nid=0x558c in Object.wait() [0x154ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04e05f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x04e05f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x00ddc800 nid=0x5e5c in Object.wait() [0x0276f000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)/*与上面一个例子相同*/
at java.lang.Object.wait(Native Method)
- waiting on <0x04ebf360> (a vipstudytest.Test$CountThread)
at java.lang.Thread.join(Thread.java:1260)
- locked <0x04ebf360> (a vipstudytest.Test$CountThread)
at vipstudytest.Test.main(Test.java:9)
"VM Thread" os_prio=2 tid=0x02cad000 nid=0x7798 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x1533ec00 nid=0x7694 waiting on condition
JNI global references: 6
sleep(long millis) 指定超时时间,例子:
public class Test {
public static void main(String[] args) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x15812800 nid=0x25f8 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x157e2400 nid=0x6ed8 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x157e1400 nid=0x46cc waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x157b7400 nid=0x4acc runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x1579a000 nid=0xccc in Object.wait() [0x15a9f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05207ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x05207ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15784400 nid=0x299c in Object.wait() [0x15a0f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05205f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x05205f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x0305c800 nid=0x2e6c waiting on condition [0x02fbf000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at vipstudytest.Test.main(Test.java:7)
"VM Thread" os_prio=2 tid=0x0315d000 nid=0x27dc runnable
"VM Periodic Task Thread" os_prio=2 tid=0x15866800 nid=0x930 waiting on condition
JNI global references: 6
parkNanos(long nanos) 例子:
import java.util.concurrent.locks.LockSupport;
public class Test{
public static void main(String[] args) {
LockSupport.parkNanos(Long.MAX_VALUE);//在规定的截止时间(单位:纳秒,1毫秒=1,000,000纳秒)之前,执行该行代码的线程被禁用。
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x14cfb400 nid=0x2030 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x14ccb400 nid=0x2104 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x14cca400 nid=0x1c9c waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x14cc7800 nid=0x2188 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x14c9b800 nid=0x1b8c in Object.wait() [0x150ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04807ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x04807ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x14c79400 nid=0x2ac0 in Object.wait() [0x1518f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04805f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x04805f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x0257cc00 nid=0x26a8 waiting on condition [0x0254f000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:338)
at Test.main(Test.java:6)
"VM Thread" os_prio=2 tid=0x14c73000 nid=0x1fb4 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x14d4d000 nid=0x24c8 waiting on condition
JNI global references: 6
parkUntil(long deadline) 例子:
import java.util.concurrent.locks.LockSupport;
public class Test{
public static void main(String[] args) {
LockSupport.parkUntil(Long.MAX_VALUE);//在规定的截止时间(单位:毫秒)之前,执行该行代码的线程被禁用。
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x154f0800 nid=0x6f18 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x15496400 nid=0x7120 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x15495800 nid=0x274c waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x154c3000 nid=0x7410 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x1547a000 nid=0x7634 in Object.wait() [0x1577f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05007ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x05007ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15464400 nid=0x6250 in Object.wait() [0x156ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x05005f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x05005f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x02dcc800 nid=0x5abc waiting on condition [0x0118f000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.parkUntil(LockSupport.java:372)
at vipstudytest.Test.main(Test.java:8)
"VM Thread" os_prio=2 tid=0x02ecd000 nid=0x7038 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x15544800 nid=0x2220 waiting on condition
JNI global references: 6
上面的jsatck 命令执行后都会出现一个TIMED_WAITING 字样 可在运行结果尾部找到
下面对这个词进行说明
Java文档官方定义TIMED_WAITING状态为:“一个线程在一个特定的等待时间内等待另一个线程完成一个动作会在这个状态”
真实生活例子:尽管充满戏剧性,你在面试中做的非常好,惊艳了所有人并获得了高薪工作。(祝贺你!)你回家告诉你的邻居你的新工作并表达你激动的心情。你的朋友告诉你他也在同一个办公楼里工作。他建议你坐他的车去上班。你想这不错。所以第一天,你走到他的房子。在他的房子前停好你的车。你等了10分钟,但你的邻居没有出现。你继续开自己的车去上班,这样你不会在第一天就迟到。这就是TIMED_WAITING.
用技术术语来解释,你是线程T1而你的邻居是线程T2。你释放了锁(这里是停止开车)并等了足足10分钟。如果你的邻居T2没有来,你继续开车。
6、TERMINATED:线程终止。
死锁:
public class Test {
// 创建两个线程之间竞争使用的对象
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new ShareThread1().start();
new ShareThread2().start();
}
static class ShareThread1 extends Thread {
public void run() {
synchronized (lock1) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("ShareThread1");
}
}
}
}
static class ShareThread2 extends Thread {
public void run() {
synchronized (lock2) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("ShareThread2");
}
}
}
}
}
Full thread dump Java HotSpot(TM) Client VM (25.131-b11 mixed mode):
"DestroyJavaVM" #10 prio=5 os_prio=0 tid=0x0008cc00 nid=0x1c8c waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Thread-1" #9 prio=5 os_prio=0 tid=0x14d5b400 nid=0x1fcc waiting for monitor entry [0x152ef000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Test$ShareThread2.run(Test.java:35)
- waiting to lock <0x048b9ef8> (a java.lang.Object)
- locked <0x048b9f00> (a java.lang.Object)
"Thread-0" #8 prio=5 os_prio=0 tid=0x14d5a400 nid=0x1cdc waiting for monitor entry [0x1565f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Test$ShareThread1.run(Test.java:20)
- waiting to lock <0x048b9f00> (a java.lang.Object)
- locked <0x048b9ef8> (a java.lang.Object)
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x14cf1800 nid=0x155c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x14ccb400 nid=0x14b4 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x14cca400 nid=0x1e58 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x14cc7400 nid=0xf8c runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x14c9f400 nid=0x1c40 in Object.wait() [0x14eaf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04807ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x04807ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x14c79400 nid=0x1f14 in Object.wait() [0x14f7f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x04805f68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x04805f68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x14c73000 nid=0x1e08 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x14cd3c00 nid=0xd08 waiting on condition
JNI global references: 6
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x14c7df8c (object 0x048b9ef8, a java.lang.Object),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x14c7d26c (object 0x048b9f00, a java.lang.Object),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at Test$ShareThread2.run(Test.java:35)
- waiting to lock <0x048b9ef8> (a java.lang.Object)
- locked <0x048b9f00> (a java.lang.Object)
"Thread-0":
at Test$ShareThread1.run(Test.java:20)
- waiting to lock <0x048b9f00> (a java.lang.Object)
- locked <0x048b9ef8> (a java.lang.Object)
Found 1 deadlock.
/*上部就是死锁的情况 "Thread-1":
waiting to lock monitor 0x14c7df8c (object 0x048b9ef8, a java.lang.Object),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x14c7d26c (object 0x048b9f00, a java.lang.Object),
which is held by "Thread-1"*/
如何确定Java程序中哪个最消耗资源
1、Windows操作系统:
a、执行Java程序,如下代码:
public class Test {
public static void main(String[] args) {
new Thread(new Task()).start();
}
static class Task implements Runnable {
@Override
public void run() {
while (true) {
}
}
}
}
b、执行jps命令获取到Java程序的进程id
c、安装并启动进程管理:下载地址:http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx——>解压运行,如下图所示:
d、在上图寻找进程id为6860的进程(上图的蓝框)——>鼠标浮在该进程信息上——>鼠标右键——>选择“Properties...”,出现下图:
我们看到TID为15900的线程最耗资源
f、执行jstack 6860命令输出6860进程内各线程信息——>在该结果中寻找nid为转化成十进制为(15900)的线程