[JAVA学习笔记-82]Thread states in JAVA

xiaoxiao2021-02-28  96

JAVA doc的说明:[https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html] public static enum Thread.State extends Enum<Thread.State> A thread state. A thread can be in one of the following states: [NEW] A thread that has not yet started is in this state.(After new it but not start) [RUNNABLE] A thread executing in the Java virtual machine is in this state(After call start() function). 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. 【JVM似乎不像OS那样区分就绪与运行两个状态,JVM中runnable状态的线程,可能是就绪(等待调度获取CPU),也可能是运行的状态, JVM统统划分为执行态。】 [BLOCKED] A thread that is blocked waiting for a monitor lock is in this state. 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. 【线程在等待object锁(synchronized),或者在调用Object.wait()之后,挂起并释放了锁,在被notify后,等待重新获取锁(reenter)】 [WAITING] A thread that is waiting indefinitely for another thread to perform a particular action is in this state. 【等待的状态与BLOCKED有点类似,BLOCKED是等待获取到锁,WAITING是等待另外一个线程指定操作唤醒,相同点是线程都被挂起,且都 不持有锁(不持有正在竞争的锁,或已经持有但释放的锁),不同点是线程进入WAITING状态,说明它已经进入了代码块。 Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods: Object.wait with no timeout Thread.join with no timeout 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. [TIMED_WAITING] A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. 【与WAITING相比,加入了超时的控制,包括sleep。与时间有关的线程挂起,进入此状态。 Thread.sleep Object.wait with timeout Thread.join with timeout LockSupport.parkNanos LockSupport.parkUntil [TERMINATED] A thread that has exited is in this state. The thread has completed execution. A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.【注意是JVM的线程状态,区别OS的线程状态】 <<LockSupport>> public class LockSupport extends Object lock以及其它同步类的线程阻塞原语。Basic thread blocking primitives for creating locks and other synchronization classes. 举个例子: static void park() Disables the current thread for thread scheduling purposes unless the permit is available. <<参考资料>> 1、有个cnblog写的挺不错的:http://www.cnblogs.com/zhengyun_ustc/archive/2013/03/18/tda.html
转载请注明原文地址: https://www.6miu.com/read-77645.html

最新回复(0)