Java的阻塞、等待及相关方法
跳到导航
跳到搜索
前言
刚刚正在梳理Java并发部分的知识点,(《Java核心技术卷1》与以前做的笔记),发现这部分出入比较大。所以单开一个词条,用于相关笔记的整理。【2020/10/19 20:50:10】
关于线程状态
线程(进程)的运行状态,最基本的三种:“就绪”、“运行”、“阻塞”。在此基础上,不同的系统进行了扩展(如:“就绪”、“死亡”、“等待”、“挂起”)形成了五状态、六状态、甚至七状态模型。
而根据其“Thread”源码可以发现,其线程公有六种状态:“New”、“Runnable”、“Blocked”、“Waiting”、“Timed_Waiting”、“Terminated”。
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;
}
These states are virtual machine states which do not reflect any operating system thread states.
- 这些状态是虚拟机状态,不反映任何操作系统线程状态。
- (网上查到的相关内容,很多都按五种状态分析,可能没看源码,按惯性思维来了,或者1.5以前版本是那样?)
其中:
- “Runnable”可以理解为“就绪”+“运行”;
- “Blocked”:用于标识线程等待一个监视器锁;
- 如:【?】
- 进入 synchronized 标记的代码块或方法;
- 调用“Object.wait”之后,重入synchronized 标记的代码块或方法;
- “Waiting”:用于标识线程等待另一个线程执行特定操作;
- 如:
- “Object.wait()”
- “Thread.join()”
- “LockSupport.park()”
- “Timed_Waiting”:具有指定等待时间的“Waiting”状态
- 如:(方法带有时间参数)
- “Object.wait(long timeout)”
- “Thread.join(long millis)”
- “Thread.sleep(long millis)”
- “LockSupport.parkNanos(long nanos)”
- “LockSupport.parkUntil(long deadline)”
- (并非像网上所说,“阻塞”分为:“同步阻塞”、“等待阻塞”、“其他阻塞”)
相关方法
- 只有在线程running的时候,才会获取cpu片段
需要区分的几个方法:
- Object.wait:释放cpu,释放锁,进入线程等待池中等待被再次唤醒(notify随机唤醒,notifyAll全部唤醒,线程结束自动唤醒),即放入锁池中竞争同步锁。
- Thread.sleep:(静态方法)释放cpu,保持锁(见源码注释“The thread does not lose ownership of any monitors”)。
- Thread.yeild:(静态方法)使当前线程让出CPU,进入线程池等待分配CPU。
- Thread.join:让当前线程等待指定线程终结。(源码中调用了“Object.wait”,即“释放cpu,释放锁”)
- LockSupport.park: