对于处于 Activity 状态但既不运行也不可运行的线程,有 3 种状态:-
- sleep
- 已屏蔽
- 等待
当线程执行 sleep() 方法时,它会在其参数指定的时间段(例如几毫秒)内从运行状态进入休眠状态。
当一个线程正在等待某个对象上的锁,而该对象由于同步方法或 block 而被其他线程获取时,该线程就会被该线程阻塞。
那么,我们可以说一个线程在其他线程上执行 wait() 时就进入了 WAITING 状态吗?
在某个线程上调用 join() 也是如此。
那么,我们是否可以说 wait()(来自 java.lang.Object)和 join()(来自 java.lang.Thread)都将线程的状态转变为 WAITING?
最佳答案
这在 javadoc of Thread.State 中有描述。 :
public static final Thread.State 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.
关于java - 执行线程是否通过同时执行 wait() 和 join() 进入 "waiting"状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8818501/