/**
* A thread state. A thread can be in one of the following states:
* 一个线程状态。 一个线程可以是下面状态中的其中一个
* <li>{@link #NEW} 第一个状态:NEW
* A thread that has not yet started is in this state.
一个线程,还没有开始运行的时,就是NEW状态
* </li>
* <li>{@link #RUNNABLE}<br> 第二个状态:RUNNABLE
* A thread executing in the Java virtual machine is in this state.
当一个线程在JVM中运行的时候,就是RUNNABLE状态
* </li>
* <li>{@link #BLOCKED}<br> 第三个状态:BLOCKED
* A thread that is blocked waiting for a monitor lock
* is in this state.
当一个线程被阻塞,在等待monitor对象锁时,就是blocked状态
* </li>
* <li>{@link #WAITING}<br> 第四个状态:WAITTING
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
当一个线程正在无限期地等待另一个线程执行特定操作的,就是WATING状态
* </li>
* <li>{@link #TIMED_WAITING}<br> 第五个状态: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.
* </li>
当一个线程正在指定时间内等待另一个线程执行某个操作,在这段时间内该线程就是
TIMED_WAITING状态
* <li>{@link #TERMINATED}<br> 第六个状态:TERMINATED
* A thread that has exited is in this state.
当一个线程已经退出了,就是TERMINATED状态
* </li>
* </ul>
*
* <p>
* 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.
一个线程在给定的时间点只会有一个状态。
这些状态是在虚拟机上线程的状态,不是反应操作系统的线程状态
*
* @since 1.5
* @see #getState
*/
public enum State {
/**
* Thread state for a thread which has not yet started.
尚未启动的线程的线程状态,为NEW
*/
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;
}
不行本来想自己翻译来着,但是发现效率太低了
0、Thread下有个静态的内部枚举类,就叫做State,里面定义了线程的状态。ps:我也不知道网上那些其他的状态是从哪里来的,简直就是坑爹呀
1、Thread类中有一个实例方法:getState(),返回的就是Thread.State状态
2、Thread在运行周期中(生命周期), 有6种状态, 在 java.lang.Thread.State 中有详细定义和说明: (一个默认静态的内部枚举类(interface在内部时,默认也是static的,就是上面我贴的代码)
a、初始(NEW):新创建了一个线程对象,但还没有调用start()方法,此时的状态是NEW
b、运行(RUNNABLE):Java线程准备就绪(ready)和运行中(running)两种状态笼统的成为“runnable”。所以这个"运行",不一定是真的在运行, 很有可能是在等待CPU资源(那就是ready),当cpu资源一到位随时可以执行,另一个表示在真正的执行中(就是running),所以READY与RUNNING都代表的是RUNNABLE状态
c、阻塞(BLOCKED):线程被阻塞,等待对象锁被释放
一般是线程等待获取一个对象锁,来继续执行下一步的操作,比较经典的就是synchronized
关键字,这个关键字修饰的代码块或者方法,均需要获取到对应的对象锁,在未获取之前,其线程的状态就一直是BLOCKED,如果线程长时间处于这种状态下,我们就是当心看是否出现死锁的问题了,死锁这样的bug简直就是坑啊
d、等待(WAITING):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)
Object.wait()
Thread.join()
LockSupport.park()
当一个线程执行了Object.wait()的时候,它一定在等待另一个线程执行Object.notify()或者Object.notifyAll()
当一个线程thread其在主线程中被执行了thread.join()的时候,主线程即会等待该工作线程执行完成。
当一个线程执行了LockSupport.park()的时候,其在等待执行LockSupport.unpark(thread)。当该线程处于这种等待的时候,其状态即为WAITING。需要关注的是,这边的等待是没有时间限制的,当发现有这种状态的线程的时候,若其长时间处于这种状态,也需要关注下程序内部有无逻辑异常(很容易出bug啊)
e、超时等待(TIME_WAITING):该状态不同于WAITING,它可以在指定的时间内自动唤醒,嘿嘿
这个状态和WAITING状态的区别就是,这个状态的等待是有一定时效的,即可以理解为WAITING状态等待的时间是永久的,即必须等到某个条件符合才能继续往下走,否则线程永远不会被唤醒。但是TIMED_WAITING,等待一段时间之后,会唤醒线程去重新获取锁。当执行如下代码的时候,对应的线程会进入到TIMED_WAITING状态
- Thread.sleep(long)
- Object.wait(long)
- Thread.join(long)
- LockSupport.parkNanos()
- LockSupport.parkUntil()
f、终止(TERMINATED):表示该线程已经执行完毕
经典的线程状态图
参考文章:https://www.cnblogs.com/GooPolaris/p/8079490.html 这篇文章,老大写的很细致,帮助我去更深的理解线程的状态
https://www.cnblogs.com/GooPolaris/p/8079490.html 我今天又看一遍兄弟写的文章,尼玛,相当的细腻,我兴奋起来了
http://www.cnblogs.com/happy-coder/p/6587092.html 这篇文章认为线程调用wait()的时候是会释放掉对象锁,不然唤醒线程怎么执行notify()方法,卧槽,两篇有点懵逼了,这就是二手资料带来的问题,看来书还得去巩固一下,wait()调用后,当前的线程肯定会释放掉锁,notify()的方法不会马上释放锁,而是告诉调用wait()的线程,你可以继续参与竞争锁了
https://www.cnblogs.com/tiancai/p/8855125.html 这篇文章也不错啊
线程的状态转换以及基本操作 https://www.jianshu.com/p/f65ea68a4a7f 这篇文章不错
https://www.jianshu.com/p/ec94ed32895f 线程状态写的细致,我觉得这个状态文章,很不错