java中线程的状态以及线程栈分析
java中线程的状态状态说明NEW初始状态。线程刚刚被创建,并且start()方法还未被调用RUNNABLE运行状态。表示线程正在java虚拟机中执行,但是可能正在等待操作系统的其他资源,比如CPUBLOCKED阻塞状态。表示线程正在等待监视器锁。表示线程正在等待获取监视器锁,以便进入同步方
java中线程的状态
状态 | 说明 |
---|---|
NEW | 初始状态。线程刚刚被创建,并且start()方法还未被调用 |
RUNNABLE | 运行状态。表示线程正在java虚拟机中执行,但是可能正在等待操作系统的其他资源,比如CPU |
BLOCKED | 阻塞状态。表示线程正在等待监视器锁。表示线程正在等待获取监视器锁,以便进入同步方法或者同步代码快,也有可能是从wait()方法被唤醒而等待再次进入同步方法或者同步代码块 |
WAITING | 等待状态。表示当前线程需要等待其他线程执行一些特殊操作,比如当前线程调用了a.wait()方法,它正在等待其他线程调用a.notify或a.notifyAll方法;如果当前线程调用了threada.join(),那么它在等待threada执行完成 |
TIMED_WAITING | 超时等待。与WAITING的不同在于,该状态有超时时间 |
TERMINATED | 终止状态,表示当前线程已经执行完毕 |
详细介绍:参考Thread内部类:State
WAITING与TIMED_WAITING:
调用以下三个方法会进入WAITING状态:
* Object.wait() 不设置超时时间
* Thread.join() 不设置超时时间
* LockSupport.park() 不设置超时时间
调用下面的方法会进入TIMED_WAITING状态:
* Object.wait(time)
* Thread.join(time)
* Thread.sleep(time)
* LockSupport.parkNanos(time)
* LockSupport.parkUntil(time)
通过jstack查看线程状态
jstack 是一个可以用来打印java进程中的线程堆栈信息的工具。一般都位于jdk安装目录的bin目录下。
一般,jstack都用来检测死锁,以及分析当前程序运行状态。
使用方式
jstack [options] pid
pid:即进程id,可以通过jps命令查看
options:可选选项。一般常用的是-l和-m,-l用于打印详细的堆栈信息,包括当前线程获得到的同步器(AbstractOwnableSynchronizer)。-m则会打印包含C++或C的堆栈信息。
例如:
命令:jstack 10968
则会把进程号为10968的进程所包含的线程信息打印出来。
其中的部分结果如下:
"main" #1 prio=5 os_prio=0 tid=0x00007fab88009800 nid=0x2add waiting on condition [0x00007fab8ee92000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304)
at test.DeadLockTest.main(DeadLockTest.java:17)
命令:jstack -l 10968
则会包含当前线程获取到的同步器。
其中的部分结果如下:
"main" #1 prio=5 os_prio=0 tid=0x00007fab88009800 nid=0x2add waiting on condition [0x00007fab8ee92000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304)
at test.DeadLockTest.main(DeadLockTest.java:17)
Locked ownable synchronizers:
- None
可以看到,带-l参数的,在线程信息后面有个’ Locked ownable synchronizers:’信息。
关于jstack的使用,下面两篇文章写的很不错,非常建议一看:
1. java-thread-dumps
2. java-thread-dumps-2
不同线程状态在jstack信息中形式
我们来看一下,当线程处于不同状态的时候,通过jstack看到的是什么样子的。
WAITING状态通过上面的讲解我们知道,线程进入waiting状态可以通过三种方式:1.Object.wait,2.Thread.join,3.LockSupport.park ,那么我们分别来看下,当程序分别通过这三种方式进入WAITING状态的时候,对应的jstack信息是怎样的。
1.Object.wait方式
代码:
public class WaitingTest {
public static void main(String[] args) throws InterruptedException {
// Object.wait
synchronized (WaitingTest.class) {
WaitingTest.class.wait();
}
}
}
jstack 结果(省略其他部分,下同):
"main" #1 prio=5 os_prio=0 tid=0x00007f384c009800 nid=0x2d9c in Object.wait() [0x00007f385268b000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6ddb9a0> (a java.lang.Class for test.WaitingTest)
at java.lang.Object.wait(Object.java:502)
at test.WaitingTest.main(WaitingTest.java:8)
- locked <0x00000000d6ddb9a0> (a java.lang.Class for test.WaitingTest)
2.Thread.join方式
代码:
public static void main(String[] args) throws InterruptedException {
// Thread.join
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("run");
try {
Thread.sleep(1000 * 300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
// 当前线程调用thread线程的join方法
thread.join();
// 只有thread的run方法执行完成之后,才会执行输出
System.out.println("Current thread running");
}
jstack:
"main" #1 prio=5 os_prio=0 tid=0x00007f88cc009800 nid=0x2f10 in Object.wait() [0x00007f88d5b73000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6ddddf8> (a test.WaitingTest$1)
at java.lang.Thread.join(Thread.java:1245)
- locked <0x00000000d6ddddf8> (a test.WaitingTest$1)
at java.lang.Thread.join(Thread.java:1319)
at test.WaitingTest.main(WaitingTest.java:23)
Locked ownable synchronizers:
- None
3.LockSupport.park方式
代码:
public static void main(String[] args) throws InterruptedException {
LockSupport.park();
}
jstack:
TIMED_WAITING
"main" #1 prio=5 os_prio=0 tid=0x00007f6efc009800 nid=0x2f88 waiting on condition [0x00007f6f034dd000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304)
at test.WaitingTest.main(WaitingTest.java:8)
Locked ownable synchronizers:
- None
1.Object.wait(time)
代码:
public static void main(String[] args) throws InterruptedException {
synchronized (WaitingTest.class) {
WaitingTest.class.wait(1000 * 50);
}
}
jstack:
"main" #1 prio=5 os_prio=0 tid=0x00007fd598009800 nid=0x301b in Object.wait() [0x00007fd59eea0000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6ddb9b8> (a java.lang.Class for test.WaitingTest)
at test.WaitingTest.main(WaitingTest.java:9)
- locked <0x00000000d6ddb9b8> (a java.lang.Class for test.WaitingTest)
Locked ownable synchronizers:
- None
2.Thread.join(time)
代码:
public static void main(String[] args) throws InterruptedException {
// Thread.join
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("run");
try {
Thread.sleep(1000 * 300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
// 当前线程调用thread线程的join方法
thread.join(1000 * 200);
// 只有thread的run方法执行完成之后,才会执行输出
System.out.println("Current thread running");
}
jstack:
"main" #1 prio=5 os_prio=0 tid=0x00007f024c009800 nid=0x365c in Object.wait() [0x00007f0254768000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6dddf40> (a test.WaitingTest$1)
at java.lang.Thread.join(Thread.java:1253)
- locked <0x00000000d6dddf40> (a test.WaitingTest$1)
at test.WaitingTest.main(WaitingTest.java:25)
Locked ownable synchronizers:
- None
3.Thread.sleep
代码:
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().sleep(1000 * 500);
}
jstack:
"main" #1 prio=5 os_prio=0 tid=0x00007f54a8009800 nid=0x36eb waiting on condition [0x00007f54ae463000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at test.WaitingTest.main(WaitingTest.java:8)
Locked ownable synchronizers:
- None
4.LockSupport.parkNanos(time) 和 LockSupport.parkUntil(time) 两个方法类似
代码:
public static void main(String[] args) throws InterruptedException {
LockSupport.parkNanos(Long.MAX_VALUE);
}
jstack:
BLOCKED状态
"main" #1 prio=5 os_prio=0 tid=0x00007eff98009800 nid=0x37b6 waiting on condition [0x00007effa0141000]
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.WaitingTest.main(WaitingTest.java:8)
Locked ownable synchronizers:
- None
BLOCKED状态,表示线程正在等待监视器锁。我们可以通过一个死锁的例子来分析。
例子很简单,程序中有两个Waiter线程,WaiterA和WaitB,它们都需要获取两个锁:lock_1和lock_2,但是它们获取锁的顺序不同,因此导致了死锁。
代码:
public class WaitingTest {
private static Object lock_1 = new Object();
private static Object lock_2 = new Object();
public static void main(String[] args) throws InterruptedException {
new WaiterA().start();
new WaiterB().start();
}
static class WaiterA extends Thread {
@Override
public void run() {
// 先获取lock_1,然后再获取lock_2
synchronized (lock_1) {
System.out.println("lock1 get,wait to get lock2");
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e1) {
}
synchronized (lock_2) {
System.out.println("lock2 get");
try {
Thread.sleep(1000 * 50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
static class WaiterB extends Thread {
@Override
public void run() {
// 先获取lock_2,然后再获取lock_1
synchronized (lock_2) {
System.out.println("lock2 get,wait to get lock1");
synchronized (lock_1) {
System.out.println("lock1 get");
try {
Thread.sleep(1000 * 50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
jstack:
"Thread-1" #10 prio=5 os_prio=0 tid=0x00007f0d800dc800 nid=0x39da waiting for monitor entry [0x00007f0d47bfa000]
java.lang.Thread.State: BLOCKED (on object monitor)
at test.WaitingTest$WaiterB.run(WaitingTest.java:47)
- waiting to lock <0x00000000d6ddc510> (a java.lang.Object)
- locked <0x00000000d6ddc520> (a java.lang.Object)
Locked ownable synchronizers:
- None
"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f0d800da800 nid=0x39d9 waiting for monitor entry [0x00007f0d47cfb000]
java.lang.Thread.State: BLOCKED (on object monitor)
at test.WaitingTest$WaiterA.run(WaitingTest.java:27)
- waiting to lock <0x00000000d6ddc520> (a java.lang.Object)
- locked <0x00000000d6ddc510> (a java.lang.Object)
Locked ownable synchronizers:
- None
可以看到,上面Thread-1 和Thread-0的状态都是Blocked,原因是它们都在等待监视器锁,而Thread-1 已经获取到了锁0x00000000d6ddc520,正在等待获取0x00000000d6ddc510锁,而0x00000000d6ddc510锁已经被Thread-0获取到了,Thread-0反过来又正在等待获取0x00000000d6ddc520,而这个锁又被Thread-1获取了。
在jstack的最后,还有如下信息:
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x00007f0d50004ed8 (object 0x00000000d6ddc510, a java.lang.Object),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x00007f0d500062c8 (object 0x00000000d6ddc520, a java.lang.Object),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at test.WaitingTest$WaiterB.run(WaitingTest.java:47)
- waiting to lock <0x00000000d6ddc510> (a java.lang.Object)
- locked <0x00000000d6ddc520> (a java.lang.Object)
"Thread-0":
at test.WaitingTest$WaiterA.run(WaitingTest.java:27)
- waiting to lock <0x00000000d6ddc520> (a java.lang.Object)
- locked <0x00000000d6ddc510> (a java.lang.Object)
Found 1 deadlock.
就是说,已经发现了一个死锁,还有就是关于死锁的详细说明。
RUNNABLE状态我们写一个BIO的server端,server端绑定一个端口,并通过accept()方法等待客户端链接。
代码:
public static void main(String[] args) throws InterruptedException, IOException {
ServerSocket serverSocket = new ServerSocket(9999);
while (true) {
Socket socket = serverSocket.accept();
// do something else
// ....
}
}
jstack:
"main" #1 prio=5 os_prio=0 tid=0x00007f4b58009800 nid=0x3ab8 runnable [0x00007f4b5ff64000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at test.WaitingTest.main(WaitingTest.java:16)
Locked ownable synchronizers:
- None
可以看到,此时虽然程序在等待客户端接入,但是程序状态依然是RUNNABLE的。
TERMINATED和NEW状态这两个状态分别表示线程生命终结和生命开始,在jstack中没有对应的信息显示。
总结:- WAITING和TIMED_WAITING的主要不同就在于后者在等待的时候设置了超时时间
- 通过Object.wait,Thread.join两种方式进入的WAITING或者TIMED_WAITING,通过jstack查看显示的都是wait on monitor,而通过其他方式(Thread.sleep,LockSupport等)进入的WAITING或者TIMED_WAITING,通过jstack查看显示的都是waiting on condition。
- BLOCKED状态,通过jstack查看,显示的是waiting for monitor entry,注意与WAITING和TIMED_WAITING的区别。
java线程状态之间的转换
java线程通过不同的方法调用进入不同的状态,下面图描述的很清楚:
参考资料
- https://sites.google.com/site/threaddumps/java-thread-dumps
- https://sites.google.com/site/threaddumps/java-thread-dumps-2
- 图片来源:http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html
顺便推荐个很好的网站:
Java中的UML图:http://www.uml-diagrams.org/examples/java-uml-examples.html
更多推荐
所有评论(0)