一 源码分析
在Thread.java类中,有一个枚举类State描述了线程状态机:
/**
* A thread state. A thread can be in one of the following states:
* <ul>
* <li>{@link #NEW}<br>
* A thread that has not yet started is in this state.
* </li>
* <li>{@link #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
* </li>
* <li>{@link #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
* </li>
* <li>{@link #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
* </li>
* <li>{@link #TIMED_WAITING}<br>
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
* </li>
* <li>{@link #TERMINATED}<br>
* A thread that has exited is in this state.
* </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,
/**
* 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;
}
1. NEW
新建状态:还没有启动,对应代码:
Thread thread1 = new Thread();
此时thread的状态就是new
2. RUNNABLE
运行状态:可能当前已经被cpu调度,也可能正在等待cpu调度,对应代码:
Thread thread1 = new Thread();
thread1.start();
3. BLOCKED
阻塞状态:当前线程等待monitor锁资源进入synchronized方法。
4. WAITING
等待状态,包含三种场景:
<1> Object.wait,不带timeout参数
<2> Thread.join,不带timeout参数
<3> LockSupport.park()
5. TIMED_WAITING
等待状态,包含以下场景:
<1> Thread.sleep
<2> Object.wait,带timeout参数
<3> Thread.join,带timeout参数
<4> LockSupport.parkNanos
<5> LockSupport.parkUntil
6. TERMINATED
终结状态:run方法执行结束
二 问题思考
1. Runnable状态的时候线程的run方法一定是在执行么?
不是,Runnable包括了Runnable和Running两个状态。只有当Running状态的时候线程才真正被cpu调度执行。
2. LinkedBlockingDeque的阻塞原理是什么?
查看源码,通过take方法从队列获取事件的时候,如果队列不为空,则取出第一条返回,否则开始阻塞,如下源码:
public E takeFirst() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lock();
try {
E x;
while ( (x = unlinkFirst()) == null)
notEmpty.await();
return x;
} finally {
lock.unlock();
}
}
await的代码实现在AbstractQueuedSynchronizer.java中:
/**
* Implements interruptible condition wait.
* <ol>
* <li> If current thread is interrupted, throw InterruptedException.
* <li> Save lock state returned by {@link #getState}.
* <li> Invoke {@link #release} with
* saved state as argument, throwing
* IllegalMonitorStateException if it fails.
* <li> Block until signalled or interrupted.
* <li> Reacquire by invoking specialized version of
* {@link #acquire} with saved state as argument.
* <li> If interrupted while blocked in step 4, throw InterruptedException.
* </ol>
*/
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
最终通过 LockSupport.park(this)实现阻塞当前线程,对应Tread状态是WAITING。
3. Android Handler,如果消息队列没有新事件到来,线程会阻塞,这个时候线程对应的状态是什么,是WAITING么?
写一个简单的demo程序,通过HandlerThread创新一个新线程
HandlerThread mDBHandlerThread = new HandlerThread("dbhandlerthread");
mDBHandlerThread.start();
通过Android Studio—Tools—Android—Android Device Monitor查看线程状态。会发现,无论dbhandlerthread对应的消息队列是否有消息需要处理,dbhandlerthread始终都是RUNNABLE状态。这与我们常常所说的如果消息队列没有消息会发生阻塞是有出入的,该怎么来理解呢?
这个问题涉及的知识点会比较多而深,需要理解:
<1>Looper的实现原理。在最新的Android N(7.0)代码之前,Looper的消息通知机制是通过epoll + pipe来实现。从Android N版本开始,Looper的消息通知机制是通过epoll + eventd来实现,eventd是一种linux进程通信机制,相比管道更加轻量级和高效。因此需要先理解eventfd是如何高效实现Linux进程通信机制的,这也是为什么goolge选择它的理由。然后还要理解epoll的实现原理,为什么Handler的实现需要用到它。
<2>java线程实现原理:jvm每一个线程最终实现是linux pthread,弄清楚java线程的状态机肯定需要理解linux平台线程的状态。
java线程启动分析:https://blog.csdn.net/rambomatrix/article/details/81294836
因此我们决定先把这些知识点一一补齐,最后再回头来看这个问题。简而言之未完待续。