Thread类源码剖析

一、引子

说来也有些汗颜,搞了几年java,忽然发现竟然没拜读过java.lang.Thread类源码,这次特地拿出来晒一晒。本文将剖析Thread类源码(本文后面源码全部默认JDK8),并讲解一些重要的拓展点。希望对大家能有一些帮助。

本文讲解主干全部出自源码和注释,保证了权威性。(注意:网上,某些书中很多观点都是错的,过时的,片面的,所以大家一定要看源码,重要事情说N遍,看源码!看源码!看源码......)

二、JVM线程状态

在正式学习Thread类中的具体方法之前,我们先来了解一下线程有哪些状态,这个将会有助于后面对Thread类中的方法的理解。

自JDK5开始,线程包括以下6个状态,摘自Thread.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.----》JVM中的线程必须只能是以上6种状态的一种。这些状态是JVM状态并不能和操作系统线程状态互相映射。
* 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,--->线程刚创建,还未执行(start方法) /**
* 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,--->已就绪可运行的状态。处于此状态的线程是正在JVM中运行的,但可能在等待操作系统级别的资源,例如CPU时间片 /**
* 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,--->阻塞等待监视器锁。处于此状态的线程正在阻塞等待监视器锁,以进入一个同步块/方法,或者在执行完wait()方法后重入同步块/方法。 /**
* 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,--->等待。执行完Object.wait无超时参数操作,或者 Thread.join无超时参数操作(进入等待指定的线程执行结束),或者 LockSupport.park操作后,线程进入等待状态。
一般在等待状态的线程在等待其它线程执行特殊操作,例如:等待另其它线程操作Object.notify()唤醒或者Object.notifyAll()唤醒所有。 /**
* 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.sleep、Object.wait带超时时间、Thread.join带超时时间、LockSupport.parkNanos、LockSupport.parkUntil这些操作会时线程进入限时等待。 /**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;--->终止,线程执行完毕。
}

看了源码6种状态,很多人会迷惑怎么没有Running状态呢?好吧,请相信源码,不要混淆操作系统线程状态和java线程状态。JVM中的线程必须只能是以上6种状态的一种!(见上图枚举State 注释中的红色部分)。

Running其实是早期操作系统下“单线程进程”的状态,如下图:

Thread类源码剖析

 注意:上图已年久失修,不可参考!!!!

好吧,现在是不是觉得三观被颠覆...

最新JAVA(JVM)线程状态转换如下图

Thread类源码剖析

如上图,可见:RUNNABLE = 正在JVM中运行的(Running)+ 可能在等待操作系统级别的资源(Ready),例如CPU时间片

  线程创建之后,不会立即进入就绪状态,因为线程的运行需要一些条件(比如内存资源),只有线程运行需要的所有条件满足了,才进入就绪状态。

  当线程进入就绪状态后,不代表立刻就能获取CPU执行时间,也许此时CPU正在执行其他的事情,因此它要等待。当得到CPU执行时间之后,线程便真正进入运行状态。

  线程在运行状态过程中,可能有多个原因导致当前线程不继续运行下去,比如用户主动让线程睡眠(睡眠一定的时间之后再重新执行)、用户主动让线程等待,或者被同步块给阻塞,此时就对应着多个状态:time waiting(睡眠或等待一定的事件)、waiting(等待被唤醒)、blocked(阻塞)。

  当由于突然中断或者子任务执行完毕,线程就会被消亡。

三.Thread类中的方法

老规矩,先看源码注释:

/**
* A <i>thread</i> is a thread of execution in a program. The Java ---》一个“线程”是在在程序中执行的线程。Java虚拟机允许应用多个线程并发运行。
* Virtual Machine allows an application to have multiple threads of
* execution running concurrently.
* <p>
* Every thread has a priority. Threads with higher priority are--》每个线程都有优先级,优先级高的先执行。线程可能是守护线程或者不是。
* executed in preference to threads with lower priority. Each thread
* may or may not also be marked as a daemon. When code running in
* some thread creates a new <code>Thread</code> object, the new---》线程的优先级等于创建线程的优先级,当且仅当一个线程是守护线程,创建出来的线程才是守护线程
* thread has its priority initially set equal to the priority of the
* creating thread, and is a daemon thread if and only if the
* creating thread is a daemon.
* <p>
* When a Java Virtual Machine starts up, there is usually a single--》通常JVM启动,有一个非守护线程作为主线程。只有当Runtime.exit被调用或者所有非守护线程死亡时(run执行完毕并返回/抛出异常)JVM会停止运行这些线程。
* non-daemon thread (which typically calls the method named
* <code>main</code> of some designated class). The Java Virtual
* Machine continues to execute threads until either of the following
* occurs:
* <ul>
* <li>The <code>exit</code> method of class <code>Runtime</code> has been
* called and the security manager has permitted the exit operation
* to take place.
* <li>All threads that are not daemon threads have died, either by
* returning from the call to the <code>run</code> method or by
* throwing an exception that propagates beyond the <code>run</code>
* method.
* </ul>
* <p>
* There are two ways to create a new thread of execution. One is to--》两种创建线程的方法:继承Thread类/实现Runnable接口
* declare a class to be a subclass of <code>Thread</code>. This
* subclass should override the <code>run</code> method of class
* <code>Thread</code>. An instance of the subclass can then be
* allocated and started. For example, a thread that computes primes
* larger than a stated value could be written as follows:
* <hr><blockquote><pre>
* class PrimeThread extends Thread {
* long minPrime;
* PrimeThread(long minPrime) {
* this.minPrime = minPrime;
* }
*
* public void run() {
* // compute primes larger than minPrime
*  . . .
* }
* }
* </pre></blockquote><hr>
* <p>
* The following code would then create a thread and start it running:
* <blockquote><pre>
* PrimeThread p = new PrimeThread(143);
* p.start();
* </pre></blockquote>
* <p>
* The other way to create a thread is to declare a class that
* implements the <code>Runnable</code> interface. That class then
* implements the <code>run</code> method. An instance of the class can
* then be allocated, passed as an argument when creating
* <code>Thread</code>, and started. The same example in this other
* style looks like the following:
* <hr><blockquote><pre>
* class PrimeRun implements Runnable {
* long minPrime;
* PrimeRun(long minPrime) {
* this.minPrime = minPrime;
* }
*
* public void run() {
* // compute primes larger than minPrime
*  . . .
* }
* }
* </pre></blockquote><hr>
* <p>
* The following code would then create a thread and start it running:
* <blockquote><pre>
* PrimeRun p = new PrimeRun(143);
* new Thread(p).start();
* </pre></blockquote>
* <p>
* Every thread has a name for identification purposes. More than--》每个线程有自己的名称用来标识自己。但可能多个线程会重名,如果启动时没有创建名字,会自动生成一个。
* one thread may have the same name. If a name is not specified when
* a thread is created, a new name is generated for it.
* <p>
* Unless otherwise noted, passing a {@code null} argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
*
* @author unascribed --》意思是:该代码第一原作者不是我,但我实在也不知道是谁,就记作无名氏吧(版权意识)
* @see Runnable
* @see Runtime#exit(int)
* @see #run()
* @see #stop()
* @since JDK1.0
*/

  Thread类实现了Runnable接口,在Thread类中,

  关键属性

  name是表示Thread的名字,可以通过Thread类的构造器中的参数来指定线程名字,

  priority表示线程的优先级(最大值为10,最小值为1,默认值为5),

  daemon表示线程是否是守护线程,如果在main线程中创建了一个守护线程,当main方法运行完毕之后,守护线程也会随着消亡。在JVM中,垃圾收集器线程就是守护线程。

  target表示要执行的任务。

  group线程群组

  关键方法

  以下是关系到线程运行状态的几个方法:

  1)start

  start()用来启动一个线程,当调用start方法后,系统才会开启一个新的线程来执行用户定义的子任务,在这个过程中,会为相应的线程分配需要的资源。

  2)run

  run()方法是不需要用户来调用的,当通过start方法启动一个线程之后,当线程获得了CPU执行时间,便进入run方法体去执行具体的任务。注意,继承Thread类必须重写run方法,在run方法中定义具体要执行的任务。

  3)sleep

  sleep方法有两个重载版本:

 public static native void sleep(long millis) throws InterruptedException;

 public static void sleep(long millis, int nanos) throws InterruptedException; 

  sleep让线程睡眠,交出CPU,让CPU去执行其他的任务。sleep方法不会释放锁,也就是说如果当前线程持有对某个对象的锁,则即使调用sleep方法,其他线程也无法访问这个对象。sleep方法相当于让线程进入阻塞状态。

  4)yield

  调用yield方法会让当前线程交出CPU权限,让CPU去执行其他的线程。它跟sleep方法类似,同样不会释放锁。但是yield不能控制具体的交出CPU的时间,另外,yield方法只能让拥有相同优先级的线程有获取CPU执行时间的机会。

  注意,调用yield方法并不会让线程进入阻塞状态,而是让线程重回就绪状态,它只需要等待重新获取CPU执行时间,这一点是和sleep方法不一样的。

  5)join

  join方法有三个重载版本:

 join()
join(long millis) //参数为毫秒
join(long millis,int nanoseconds) //第一参数为毫秒,第二个参数为纳秒

  可以看出,当调用thread.join()方法后,main线程会进入等待,然后等待thread执行完之后再继续执行。

  实际上调用join方法是调用了Object的wait方法,这个可以通过查看源码得知:

  Thread类源码剖析

  wait方法会让线程进入阻塞状态,并且会释放线程占有的锁,并交出CPU执行权限。

  6)interrupt

  interrupt,中断。单独调用interrupt方法可以使得处于阻塞状态的线程抛出一个异常,也就说,它可以用来中断一个正处于阻塞状态的线程;

  7)stop

  stop方法已经是一个废弃的方法,它是一个不安全的方法。因为调用stop方法会直接终止run方法的调用,并且会抛出一个ThreadDeath错误,如果线程持有某个对象锁的话,会完全释放锁,导致对象状态不一致。所以stop方法基本是不会被用到的。

  8)destroy

  destroy方法也是废弃的方法。基本不会被使用到。

四、拓展点

1.LookSupport.park()和unpark()原理

LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了基本的线程同步原语。LockSupport实际上是调用了Unsafe类里的函数,归结到Unsafe里,只有两个函数:

挂起
public native void park(boolean isAbsolute, long time);

唤醒

public native void unpark(Thread jthread);

unpark函数为线程提供“许可(permit)”,park函数则等待“许可”。这个有点像信号量,但是这个“许可”是不能叠加的,“许可”是一次性的。

比如线程B连续调用了三次unpark函数,当线程A调用park函数就使用掉这个“许可”,如果线程A再次调用park,则进入等待状态。

注意,unpark函数可以先于park调用。比如线程B调用unpark函数,给线程A发了一个“许可”,那么当线程A调用park时,它发现已经有“许可”了,那么它会马上再继续运行。

实际上,park函数即使没有“许可”,有时也会无理由地返回,这点等下再解析。

park/unpark模型真正解耦了线程之间的同步,线程之间不再需要一个Object或者其它变量来存储状态,不再需要关心对方的状态。

我们从JDK源码开始看,java.util.concurrent.locks.LookSupport.park()如下:

 /**
* Disables the current thread for thread scheduling purposes unless the
* permit is available.--->停止当前线程的调度执行一直到许可可达。
*
* <p>If the permit is available then it is consumed and the call
* returns immediately; otherwise the current thread becomes disabled
* for thread scheduling purposes and lies dormant until one of three
* things happens:
*--->当许可条件满足时,当前线程会立即返回。否则会一直停止线程调度并且假死一直到下面3件事情发生:
* <ul>
*
* <li>Some other thread invokes {@link #unpark unpark} with the
* current thread as the target; or
*--->1.其它线程调用unpark方法唤醒此线程
* <li>Some other thread {@linkplain Thread#interrupt interrupts}
* the current thread; or
*--->2.其它线程中断此线程
* <li>The call spuriously (that is, for no reason) returns.
* </ul>
**--->3.此线程未知错误返回了
* <p>This method does <em>not</em> report which of these caused the
* method to return. Callers should re-check the conditions which caused
* the thread to park in the first place. Callers may also determine,
* for example, the interrupt status of the thread upon return.
*----》该方法不会告知是哪个原因导致的返回。调用方需要重新校验导致线程park的条件。比如中断状态。
*/
public static void park() {
UNSAFE.park(false, 0L);//线程调用该方法,线程将一直阻塞直到超时(这里没有超时时间为0),或者是中断条件出现。
}

这里我们就简单看一下park()源码,目录:
openjdk-8-src-b132-03_mar_2014\openjdk\hotspot\src\share\vm\runtime\park.cpp
openjdk-8-src-b132-03_mar_2014\openjdk\hotspot\src\share\vm\runtime\park.hpp
openjdk-8-src-b132-03_mar_2014\openjdk\hotspot\src\os\linux\vm\os_linux.cpp
openjdk-8-src-b132-03_mar_2014\openjdk\hotspot\src\os\linux\vm\os_linux.hpp

park.hpp:

 class Parker : public os::PlatformParker {
private:
volatile int _counter ;
Parker * FreeNext ;
JavaThread * AssociatedWith ; // Current association public:
Parker() : PlatformParker() {
_counter = 0 ;
FreeNext = NULL ;
AssociatedWith = NULL ;
}
protected:
~Parker() { ShouldNotReachHere(); }
public:
// For simplicity of interface with Java, all forms of park (indefinite,
// relative, and absolute) are multiplexed into one call.
void park(bool isAbsolute, jlong time);
void unpark(); // Lifecycle operators
static Parker * Allocate (JavaThread * t) ;
static void Release (Parker * e) ;
private:
static Parker * volatile FreeList ;
static volatile int ListLock ; };

os_linux.hpp中,PlatformParker:

 class PlatformParker : public CHeapObj<mtInternal> {
protected:
enum {
REL_INDEX = 0,
ABS_INDEX = 1
};
int _cur_index; // which cond is in use: -1, 0, 1
pthread_mutex_t _mutex [1] ;
pthread_cond_t _cond [2] ; // one for relative times and one for abs. public: // TODO-FIXME: make dtor private
~PlatformParker() { guarantee (0, "invariant") ; } public:
PlatformParker() {
int status;
status = pthread_cond_init (&_cond[REL_INDEX], os::Linux::condAttr());
assert_status(status == 0, status, "cond_init rel");
status = pthread_cond_init (&_cond[ABS_INDEX], NULL);
assert_status(status == 0, status, "cond_init abs");
status = pthread_mutex_init (_mutex, NULL);
assert_status(status == 0, status, "mutex_init");
_cur_index = -1; // mark as unused 初始化时-1未使用
}
};

可以看到Parker类实际上用Posix的mutex,condition来实现的。
在Parker类里的_counter字段,就是用来记录所谓的“许可”的。

park()源码实现,为了保证源码的完整性,就直接在源码上注释原理了。

 void Parker::park(bool isAbsolute, jlong time) {
// Ideally we'd do something useful while spinning, such
// as calling unpackTime(). // Optional fast-path check:
// Return immediately if a permit is available.
// We depend on Atomic::xchg() having full barrier semantics
// since we are doing a lock-free update to _counter.
if (Atomic::xchg(0, &_counter) > 0) return;//先尝试能否直接拿到“许可”,即_counter>0时,如果成功,则把_counter设置为0,并返回: Thread* thread = Thread::current();
assert(thread->is_Java_thread(), "Must be JavaThread");
JavaThread *jt = (JavaThread *)thread; // Optional optimization -- avoid state transitions if there's an interrupt pending.
// Check interrupt before trying to wait
if (Thread::is_interrupted(thread, false)) {
return;
} // Next, demultiplex/decode time arguments
timespec absTime;
if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all
return;
}
if (time > 0) {
unpackTime(&absTime, isAbsolute, time);
} // Enter safepoint region
// Beware of deadlocks such as 6317397.
// The per-thread Parker:: mutex is a classic leaf-lock.
// In particular a thread must never block on the Threads_lock while
// holding the Parker:: mutex. If safepoints are pending both the
// the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
ThreadBlockInVM tbivm(jt);//如果不成功,则构造一个ThreadBlockInVM, // Don't wait if cannot get lock since interference arises from
// unblocking. Also. check interrupt before trying wait
if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) {
return;
} int status ;
if (_counter > 0) { // no wait needed然后检查_counter是不是>0,如果是,则把_counter设置为0,unlock mutex并返回:
_counter = 0;
status = pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
// Paranoia to ensure our locked and lock-free paths interact
// correctly with each other and Java-level accesses.
OrderAccess::fence();
return;
} #ifdef ASSERT
// Don't catch signals while blocked; let the running threads have the signals.
// (This allows a debugger to break into the running thread.)
sigset_t oldsigs;
sigset_t* allowdebug_blocked = os::Linux::allowdebug_blocked_signals();
pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);
#endif OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
jt->set_suspend_equivalent();
// cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() assert(_cur_index == -1, "invariant");
if (time == 0) {
_cur_index = REL_INDEX; // arbitrary choice when not timed
status = pthread_cond_wait (&_cond[_cur_index], _mutex) ;
} else {
_cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
status = os::Linux::safe_cond_timedwait (&_cond[_cur_index], _mutex, &absTime) ;
if (status != 0 && WorkAroundNPTLTimedWaitHang) {
pthread_cond_destroy (&_cond[_cur_index]) ;
pthread_cond_init (&_cond[_cur_index], isAbsolute ? NULL : os::Linux::condAttr());
}
}
_cur_index = -1;
assert_status(status == 0 || status == EINTR ||
status == ETIME || status == ETIMEDOUT,
status, "cond_timedwait"); #ifdef ASSERT
pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
#endif _counter = 0 ;
status = pthread_mutex_unlock(_mutex) ;
assert_status(status == 0, status, "invariant") ;
// Paranoia to ensure our locked and lock-free paths interact
// correctly with each other and Java-level accesses.
OrderAccess::fence(); // If externally suspended while waiting, re-suspend
if (jt->handle_special_suspend_equivalent_condition()) {
jt->java_suspend_self();
}
}

unpark()源码实现

 void Parker::unpark() {
int s, status ;
status = pthread_mutex_lock(_mutex);//互斥锁加锁
assert (status == 0, "invariant") ;
s = _counter;//保存初始counter
_counter = 1;//置1
if (s < 1) {//如果原本为0
// thread might be parked线程可能被挂起
if (_cur_index != -1) {
// thread is definitely parked
if (WorkAroundNPTLTimedWaitHang) {
status = pthread_cond_signal (&_cond[_cur_index]);//唤醒在park中等待的线程
assert (status == 0, "invariant");
status = pthread_mutex_unlock(_mutex);//释放锁
assert (status == 0, "invariant");
} else {
status = pthread_mutex_unlock(_mutex);//释放锁
assert (status == 0, "invariant");
status = pthread_cond_signal (&_cond[_cur_index]);//唤醒在park中等待的线程
assert (status == 0, "invariant");
}
} else {
pthread_mutex_unlock(_mutex);//释放锁
assert (status == 0, "invariant") ;
}
} else {//如果原本为1,释放锁
pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
}
}

2.Caches缓存类

Caches-->WeakClassKey-->WeakReference

 /** cache of subclass security audit results */
/* Replace with ConcurrentReferenceHashMap when/if it appears in a future
* release */
private static class Caches {
/** cache of subclass security audit results */
static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
new ConcurrentHashMap<>(); /** queue for WeakReferences to audited subclasses */
static final ReferenceQueue<Class<?>> subclassAuditsQueue =
new ReferenceQueue<>();
}

 Caches类中包含了两个成员subclassAudits和subclasseAuditsQueue:
  subclassAudits——该成员属性提供了一个哈希表缓存,该缓存的键类型为java.lang.Thread.WeakClassKey,注意看它的值类型是一个java.lang.Boolean类型的,从其代码注释可以知道这个哈希表缓存中保存的是所有子类的代码执行安全性检测结果;
  subclassAuditsQueue——该成员属性定义了一个“Queue队列”,保存了已经审核过的子类弱引用

static class WeakClassKey extends WeakReference<Class<?>>关于弱引用WeakReference,飞机票:Java中关于WeakReference和WeakHashMap的理解


参考

《JAVA高并发程序设计》电子工业出版社

Java并发编程:Thread类的使用

上一篇:HTML5网页录音和压缩,边猜边做..(附源码)


下一篇:Ionic2+WebApi 导出Excel转Pdf文件。