Java线程状态
在Thread类中,存在一个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 {@code Object.wait()}
* on an object is waiting for another thread to call
* {@code Object.notify()} or {@code Object.notifyAll()} on
* that object. A thread that has called {@code Thread.join()}
* 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具有6种状态:
-
NEW
线程创建后,未启动时的状态 -
RUNNABLE
可运行状态。处于该状态的线程正在JVM中执行,但可能在等待操作系统资源,比如处理器资源
对于io方法,比如socket.getInputStream().read()方法,阻塞时,线程状态也是RUNNABLE"main" #1 prio=5 os_prio=0 cpu=171.88ms elapsed=7.02s tid=0x0000022bc7a3f400 nid=0x3f4c runnable [0x00000086ab2ff000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.SocketDispatcher.read0(java.base@17.0.2/Native Method) at sun.nio.ch.SocketDispatcher.read(java.base@17.0.2/SocketDispatcher.java:46) at sun.nio.ch.NioSocketImpl.tryRead(java.base@17.0.2/NioSocketImpl.java:261) at sun.nio.ch.NioSocketImpl.implRead(java.base@17.0.2/NioSocketImpl.java:312) at sun.nio.ch.NioSocketImpl.read(java.base@17.0.2/NioSocketImpl.java:350) at sun.nio.ch.NioSocketImpl$1.read(java.base@17.0.2/NioSocketImpl.java:803) at java.net.Socket$SocketInputStream.read(java.base@17.0.2/Socket.java:966) at java.io.InputStream.readNBytes(java.base@17.0.2/InputStream.java:409) at java.io.InputStream.readAllBytes(java.base@17.0.2/InputStream.java:346) at org.hzqisheng.ReadSocket.main(ReadSocket.java:15) -
BLOCKED
阻塞状态
线程处于一个阻塞状态,等待监视器锁。
等待进入synchronized块/代码的线程会进入BLOCKED状态。 -
WAITING
等待状态
调用以下方法会导致线程进入WAITING状态- 未指定timeout的Object.wait
- 未指定timeout的Thread.join
- LockSupport.park()
-
TIMED_WAITING
指定了时间的等待状态
调用以下方法会导致线程进入TIMED_WAITING状态- Thread.sleep
- Object.wait带有超时时间
- Thread.join带有超时时间
- LockSupport.parkNanos
- LockSupport.parkUntil
-
TERMINATED
已停止的线程

浙公网安备 33010602011771号