线程状态
背景
发生OOM 之后通过 jstack 获取线程信息,当看到一堆线程状态的时候会有点迷,不知道各种线程状态具体代表的是什么意义,RUNABLE 很好理解,但是 BLOCKED,TIMED_WAITING 和 WAITING 具体有什么区别呢,CPU 又是处于一个什么样的利用状态呢?
带着这个疑惑翻了下Java Thread类
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;
}
线程有NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED 这些状态
NEW
线程刚创建未调用start时 的状态
RUNNABLE
线程创建好后调用start,处于运行中或者等待CPU调度的状态
BLOCKED
阻塞在synchornized 同步代码块外面的状态
WAITING
调用object.wait thread.join LockSupport.park 时的状态
TIMED_WAITING
调用WAITING 类操作但指定了等待时间
TERMINATED
线程执行完毕的时候
测试代码
public class ThreadTest {
public static final int ONE_MINUTE_IN_MILLIS = 60000;
private static Object object = new Object();
private static Object object2 = new Object();
public static void main(String[] args) throws InterruptedException {
ThreadTest threadTest = new ThreadTest();
new Thread(() -> threadTest.waitObject(), "thread1").start();
new Thread(() -> threadTest.waitMethod(), "thread2").start();
new Thread(() -> threadTest.waitMethod(), "thread3").start();
Thread.sleep(ONE_MINUTE_IN_MILLIS * 2);
new Thread(() -> threadTest.notifyObject(), "thread4").start();
Thread.sleep(ONE_MINUTE_IN_MILLIS * 3);
}
/**
* 测试 阻塞在 object.wait时 线程的状态
*/
public void waitObject() {
synchronized (object) {
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 释放 object.wait
*/
public void notifyObject() {
synchronized (object) {
object.notify();
}
}
/**
* 测试 阻塞在 进入synchronize 代码块时 线程的状态
*/
public void waitMethod() {
synchronized (object2) {
try {
Thread.sleep(ONE_MINUTE_IN_MILLIS * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
上述代码会启动三个线程。
Thread1 测试object.await()时线程的状态。
Thread2 获取object2 的锁后 调用 Thread.sleep(two_min);测试Thread.sleep 时线程的状态。
Thread3 获取object2 锁失败会被阻塞在 synchornized 代码块外面。
如上图,代码刚运行的时候
thread1 调用object.wait 处于WAITING 状态。
thread2 调用 thread.sleep 处于 TIMED_WAITING 状态。
thread3 阻塞在synchornized 代码块外面处于 BLOCKED 状态。
一分钟后
thread2 sleep 结束,释放object2 锁。thread3 抢占到了object2 锁,从 BLOCKED 状态变成了 TIMED_WAITING。
两分钟后
由于两分钟后调用object.notify()。thread1被唤醒,从而结束线程
thread1 也因为 thread.sleep(onw_min) 的时间到了,结束线程
所以只剩这个Main 线程了
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。