publicenumState{ /** * 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. * 可运行线程的线程状态。处于可运行状态的线程正在Java虚拟机中执行,但它可能正在等待来自操作系统的其他资源,例如处理器。 */ 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 Object.wait() . * 等待监视器锁定的被阻止线程的线程状态。处于阻塞状态的线程正在等待监视器锁进入同步块/方法,或在调用对象后重新进入同步块/方法。 */ BLOCKED,
/** * Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods: * 1.Object.wait() with no timeout * 2.Thread.join() with no timeout * 3.LockSupport.park() * * A thread in the waiting state is waiting for another thread to perform a particular action. * For example, a thread that has called Object.wait() on an object is waiting for another thread to call * Object.notify or Object.notifyAll() on that object. A thread that has called 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: * 1.Thread.sleep() * 2.Object.wait() with timeout * 3.Thread.join() with timeout * 4.LockSupport.parkNanos() * 5.LockSupport.parkUntil() */ TIMED_WAITING, /** * Thread state for a terminated thread. The thread has completed execution. */ TERMINATED; }
1.1 状态1: NEW
当线程被创建出来还没有被调用 start()时候的状态
1
官方描述:Thread state for a thread which has not yet started.
示例代码:
1 2 3 4 5 6 7
publicclassThreadStateTest{ publicstaticvoidmain(String[] args){ Thread thread = new Thread("thread1"); System.out.println(thread.getState()); } } //输出: 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.
官方描述: 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 Object.wait() . 译文:等待监视器锁定的被阻止线程的线程状态。处于阻塞状态的线程正在等待监视器锁进入同步块/方法,或在调用对象后重新进入同步块/方法。
"thread2" #13 prio=5 os_prio=31 tid=0x00007f81c387e800 nid=0xa703 waiting for monitor entry [0x000070000bbbd000] java.lang.Thread.State: BLOCKED (on object monitor) at org.example.thread.BlockedState$2.run(BlockedState.java:26) - waiting to lock <0x0000000715916c40> (a java.lang.String)
"thread1" #11 prio=5 os_prio=31 tid=0x00007f81b3d9b800 nid=0x5503 runnable [0x000070000b9b7000] java.lang.Thread.State: RUNNABLE at org.example.thread.BlockedState$1.run(BlockedState.java:14) - locked <0x0000000715916c40> (a java.lang.String)
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods: 1.Object.wait with no timeout 2.Thread.join with no timeout 3.LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.
"thread1" #11 prio=5 os_prio=31 tid=0x00007fc57b877800 nid=0x5503 in Object.wait() [0x0000700011529000] java.lang.Thread.State: WAITING (on object monitor) true at java.lang.Object.wait(Native Method) true - waiting on <0x0000000715916c40> (a java.lang.Class for org.example.thread.WaitingState) true at java.lang.Object.wait(Object.java:502) true at org.example.thread.WaitingState$1.run(WaitingState.java:12) true - locked <0x0000000715916c40> (a java.lang.Class for org.example.thread.WaitingState)
官方描述: 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: 1.Thread.sleep 2.Object.wait with timeout 3.Thread.join with timeout 4.LockSupport.parkNanos 5.LockSupport.parkUntil
"thread1" #11 prio=5 os_prio=31 tid=0x00007f80a0129800 nid=0x5503 in Object.wait() [0x000070000856b000] java.lang.Thread.State: TIMED_WAITING (on object monitor) trueat java.lang.Object.wait(Native Method) true- waiting on <0x0000000715916d28> (a java.lang.Class for org.example.thread.TimeWaitingState) trueat org.example.thread.TimeWaitingState$1.run(TimeWaitingState.java:12) true- locked <0x0000000715916d28> (a java.lang.Class for org.example.thread.TimeWaitingState)