「阿里面試系列」面試加分項,從JVM層面瞭解執行緒的啟動和停止
文章簡介
這一篇主要圍繞執行緒狀態控制相關的操作分析執行緒的原理,比如執行緒的中斷,執行緒的通訊等,內容比較多,可能會分兩篇文章
阿里面試系列導讀:關注我的技術公眾號【架構師修煉寶典】一週出產1-2篇技術文章。
內容導航
- 執行緒的啟動的實現原理
- 執行緒停止的實現原理分析
- 為什麼中斷執行緒會丟擲InterruptedException的
執行緒的啟動原理
前面我們簡單分析過了執行緒的使用,通過呼叫執行緒的啟動方法來啟動執行緒,執行緒啟動後會呼叫執行方法執行業務邏輯,執行方法執行完畢後,執行緒的生命週期也就終止了。
很多同學最早學習執行緒的時候會比較疑惑,啟動一個執行緒為什麼是呼叫啟動方法,而不是執行方法,這做一個簡單的分析,先簡單看一下啟動方法的定義
public class Thread implements Runnable { ... public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0) throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this); boolean started = false; try { start0(); //注意這裡 started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } } private native void start0();//注意這裡 ...
我們看到呼叫啟動方法實際上是呼叫一個本地方法START0()來啟動一個執行緒,首先START0()這個方法是線上程的靜態塊中來註冊的,程式碼如下
public class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
這個registerNatives的作用是註冊一些本地方法提供給Thread類來使用,比如start0(),isAlive(),currentThread(),sleep();這些都是大家很熟悉的方法.registerNatives
的本地方法的定義在檔案
Thread.c定義了各個作業系統平臺要用的關於執行緒的公共資料和操作,以下是Thread.c的全部內容
static JNINativeMethod methods[] = {
{"start0", "()V", (void *)&JVM_StartThread},
{"stop0", "(" OBJ ")V", (void *)&JVM_StopThread},
{"isAlive", "()Z", (void *)&JVM_IsThreadAlive},
{"suspend0", "()V", (void *)&JVM_SuspendThread},
{"resume0", "()V", (void *)&JVM_ResumeThread},
{"setPriority0", "(I)V", (void *)&JVM_SetThreadPriority},
{"yield", "()V", (void *)&JVM_Yield},
{"sleep", "(J)V", (void *)&JVM_Sleep},
{"currentThread", "()" THD, (void *)&JVM_CurrentThread},
{"countStackFrames", "()I", (void *)&JVM_CountStackFrames},
{"interrupt0", "()V", (void *)&JVM_Interrupt},
{"isInterrupted", "(Z)Z", (void *)&JVM_IsInterrupted},
{"holdsLock", "(" OBJ ")Z", (void *)&JVM_HoldsLock},
{"getThreads", "()[" THD, (void *)&JVM_GetAllThreads},
{"dumpThreads", "([" THD ")[[" STE, (void *)&JVM_DumpThreads},
{"setNativeName", "(" STR ")V", (void *)&JVM_SetNativeThreadName},
};
#undef THD
#undef OBJ
#undef STE
#undef STR
JNIEXPORT void JNICALL
Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass cls)
{
(*env)->RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods));
}
從這段程式碼可以看出,start0(),實際會執行JVM_StartThread方法,這個方法是幹嘛的呢?從名字上來看,似乎是在JVM層面去啟動一個執行緒,如果真的是這樣,那麼在JVM層面,一定會呼叫Java中定義的執行方法。那接下來繼續去找找答案。我們找到jvm.cpp這個檔案;這個檔案需要下載hotspot的原始碼才能找到。
JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
JVMWrapper("JVM_StartThread");
...
native_thread = new JavaThread(&thread_entry, sz);
...
JVM_ENTRY是用來定義JVM_StartThread函式的,在這個函式裡面建立了一個真正和平臺有關的本地執行緒。本著打破砂鍋查到底的原則,繼續看看newJavaThread做了什麼事情,繼續尋找JavaThread的定義
在hotspot的原始碼中thread.cpp檔案中1558行的位置可以找到如下程式碼
JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
Thread()
#if INCLUDE_ALL_GCS
, _satb_mark_queue(&_satb_mark_queue_set),
_dirty_card_queue(&_dirty_card_queue_set)
#endif // INCLUDE_ALL_GCS
{
if (TraceThreadEvents) {
tty->print_cr("creating thread %p", this);
}
initialize();
_jni_attach_state = _not_attaching_via_jni;
set_entry_point(entry_point);
// Create the native thread itself.
// %note runtime_23
os::ThreadType thr_type = os::java_thread;
thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
os::java_thread;
os::create_thread(this, thr_type, stack_sz);
_safepoint_visible = false;
// The _osthread may be NULL here because we ran out of memory (too many threads active).
// We need to throw and OutOfMemoryError - however we cannot do this here because the caller
// may hold a lock and all locks must be unlocked before throwing the exception (throwing
// the exception consists of creating the exception object & initializing it, initialization
// will leave the VM via a JavaCall and then all locks must be unlocked).
//
// The thread is still suspended when we reach here. Thread must be explicit started
// by creator! Furthermore, the thread must also explicitly be added to the Threads list
// by calling Threads:add. The reason why this is not done here, is because the thread
// object must be fully initialized (take a look at JVM_Start)
}
這個方法有兩個引數,第一個是函式名稱,執行緒建立成功之後會根據這個函式名稱呼叫對應的函式;第二個是當前程序內已經有的執行緒數量。最後我們重點關注與一下os :: create_thread,實際就是呼叫平臺建立執行緒的方法來建立執行緒。
接下來就是執行緒的啟動,會呼叫Thread.cpp檔案中的Thread :: start(Thread * thread)方法,程式碼如下
void Thread::start(Thread* thread) {
trace("start", thread);
// Start is different from resume in that its safety is guaranteed by context or
// being called from a Java method synchronized on the Thread object.
if (!DisableStartThread) {
if (thread->is_Java_thread()) {
// Initialize the thread state to RUNNABLE before starting this thread.
// Can not set it after the thread started because we do not know the
// exact thread state at that time. It could be in MONITOR_WAIT or
// in SLEEPING or some other state.
java_lang_Thread::set_thread_status(((JavaThread*)thread)->threadObj(),
java_lang_Thread::RUNNABLE);
}
os::start_thread(thread);
}
}
啟動方法中有一個函式呼叫:os :: start_thread(thread);,呼叫平臺啟動執行緒的方法,最終會呼叫Thread.cpp檔案中的JavaThread :: run()方法
// The first routine called by a new Java thread
void JavaThread::run() {
// initialize thread-local alloc buffer related fields
this->initialize_tlab();
// used to test validitity of stack trace backs
this->record_base_of_stack_pointer();
// Record real stack base and size.
this->record_stack_base_and_size();
// Initialize thread local storage; set before calling MutexLocker
this->initialize_thread_local_storage();
this->create_stack_guard_pages();
this->cache_global_variables();
// Thread is now sufficient initialized to be handled by the safepoint code as being
// in the VM. Change thread state from _thread_new to _thread_in_vm
ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
assert(JavaThread::current() == this, "sanity check");
assert(!Thread::current()->owns_locks(), "sanity check");
DTRACE_THREAD_PROBE(start, this);
// This operation might block. We call that after all safepoint checks for a new thread has
// been completed.
this->set_active_handles(JNIHandleBlock::allocate_block());
if (JvmtiExport::should_post_thread_life()) {
JvmtiExport::post_thread_start(this);
}
EventThreadStart event;
if (event.should_commit()) {
event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
event.commit();
}
// We call another function to do the rest so we are sure that the stack addresses used
// from there will be lower than the stack base just computed
thread_main_inner();
// Note, thread is no longer valid at this point!
}
這個方法中主要是做一系列的初始化操作,最後有一個方法thread_main_inner,接下來看看這個方法的邏輯是什麼樣的
void JavaThread::thread_main_inner() {
assert(JavaThread::current() == this, "sanity check");
assert(this->threadObj() != NULL, "just checking");
// Execute thread entry point unless this thread has a pending exception
// or has been stopped before starting.
// Note: Due to JVM_StopThread we can have pending exceptions already!
if (!this->has_pending_exception() &&
!java_lang_Thread::is_stillborn(this->threadObj())) {
{
ResourceMark rm(this);
this->set_native_thread_name(this->get_thread_name());
}
HandleMark hm(this);
this->entry_point()(this, this);
}
DTRACE_THREAD_PROBE(stop, this);
this->exit(false);
delete this;
}
和主流程無關的程式碼咱們先不去看,直接找到最核心的程式碼塊this-> entry_point()(this,this);,這個entrypoint應該比較熟悉了,因為我們在前面提到了,在:: JavaThread這個方法中傳遞的第一個引數,代表函式名稱,執行緒啟動的時候會呼叫這個函式。
如果大家還沒有暈車的話,應該記得我們在jvm.cpp檔案中看到的程式碼,在建立native_thread = newJavaThread( &thread_entry,SZ); 的時候傳遞了一個threadentry函式,所以我們在jvm.cpp中找到這個函式的定義如下
static void thread_entry(JavaThread* thread, TRAPS) {
{
HandleMark hm(THREAD);
Handle obj(THREAD, thread->threadObj());
JavaValue result(T_VOID);
JavaCalls::call_virtual(&result,
obj,
KlassHandle(THREAD, SystemDictionary::Thread_klass()),
vmSymbols::run_method_name(), //注意這裡
vmSymbols::void_method_signature(),
THREAD);
}
可以看到vmSymbols :: run_method_name()這個呼叫,其實就是通過回撥方法呼叫Java執行緒中定義的執行方法,run_method_name是一個巨集定義,在vmSymbols.hpp檔案中可以找到如下程式碼
#define VM_SYMBOLS_DO(template, do_alias)
...
template(run_method_name, "run")
...
所以結論就是,Java的裡面建立執行緒之後必須要呼叫啟動方法才能真正的建立一個執行緒,該方法會呼叫虛擬機器啟動一個本地執行緒,本地執行緒的建立會呼叫當前系統建立執行緒的方法進行建立,並且執行緒被執行的時候會回撥跑方法進行業務邏輯的處理
執行緒的終止方法及原理
執行緒的終止有主動和被動之分,被動表示執行緒出現異常退出或者執行方法執行完畢,執行緒會自動終止。主動的方式是Thread.stop()來實現執行緒的終止,但是停止()方法是一個過期的方法,官方是不建議使用,理由很簡單,停止()方法在中介一個執行緒時不會保證執行緒的資源正常釋放,也就是不會給執行緒完成資源釋放工作的機會,相當於我們在Linux的上通過kill -9強制結束一個程序。
那麼如何安全的終止一個執行緒呢?
我們先看一下下面的程式碼,程式碼演示了一個正確終止執行緒的方法,至於它的實現原理,稍後我們再分析
public class InterruptedDemo implements Runnable{
@Override
public void run() {
long i=0l;
while(!Thread.currentThread().isInterrupted()){//notice here
i++;
}
System.out.println("result:"+i);
}
public static void main(String[] args) throws InterruptedException {
InterruptedDemo interruptedDemo=new InterruptedDemo();
Thread thread=new Thread(interruptedDemo);
thread.start();
Thread.sleep(1000);//睡眠一秒
thread.interrupt();//notice here
}
}
程式碼中有兩處需要注意,在主執行緒中,呼叫了執行緒的interrupt()方法,在執行方法中,而迴圈中通過Thread.currentThread()。isInterrupted()來判斷執行緒中斷的標識。所以我們在這裡猜想一下,應該是線上程中維護了一箇中斷標識,通過thread.interrupt()方法去改變了中斷標識的值使得執行方法中而迴圈的判斷不成立而跳出迴圈,因此執行方法執行完畢以後執行緒就終止了。
執行緒中斷的原理分析
我們來看一下thread.interrupt()方法做了什麼事情
public class Thread implements Runnable {
...
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0();
}
...
這個方法裡面,呼叫了中斷0(),這個方法在前面分析啟動方法的時候見過,是一個本機方法,這裡就不再重複貼程式碼了,同樣,我們找到jvm.cpp檔案,找到JVM_Interrupt的定義
JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
JVMWrapper("JVM_Interrupt");
// Ensure that the C++ Thread and OSThread structures aren't freed before we operate
oop java_thread = JNIHandles::resolve_non_null(jthread);
MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
// We need to re-resolve the java_thread, since a GC might have happened during the
// acquire of the lock
JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
if (thr != NULL) {
Thread::interrupt(thr);
}
JVM_END
這個方法比較簡單,直接呼叫了Thread :: interrupt(thr)這個方法,這個方法的定義在Thread.cpp檔案中,程式碼如下
void Thread::interrupt(Thread* thread) {
trace("interrupt", thread);
debug_only(check_for_dangling_thread_pointer(thread);)
os::interrupt(thread);
}
Thread :: interrupt方法呼叫了os :: interrupt方法,這個是呼叫平臺的中斷方法,這個方法的實現是在os _ * .cpp檔案中,其中星號代表的是不同平臺,因為jvm是跨平臺的,所以對於不同的操作平臺,執行緒的排程方式都是不一樣的。我們以os_linux.cpp檔案為例
void os::interrupt(Thread* thread) {
assert(Thread::current() == thread || Threads_lock->owned_by_self(),
"possibility of dangling Thread pointer");
//獲取本地執行緒物件
OSThread* osthread = thread->osthread();
if (!osthread->interrupted()) {//判斷本地執行緒物件是否為中斷
osthread->set_interrupted(true);//設定中斷狀態為true
// More than one thread can get here with the same value of osthread,
// resulting in multiple notifications. We do, however, want the store
// to interrupted() to be visible to other threads before we execute unpark().
//這裡是記憶體屏障,這塊在後續的文章中會剖析;記憶體屏障的目的是使得interrupted狀態對其他執行緒立即可見
OrderAccess::fence();
//_SleepEvent相當於Thread.sleep,表示如果執行緒呼叫了sleep方法,則通過unpark喚醒
ParkEvent * const slp = thread->_SleepEvent ;
if (slp != NULL) slp->unpark() ;
}
// For JSR166. Unpark even if interrupt status already was set
if (thread->is_Java_thread())
((JavaThread*)thread)->parker()->unpark();
//_ParkEvent用於synchronized同步塊和Object.wait(),這裡相當於也是通過unpark進行喚醒
ParkEvent * ev = thread->_ParkEvent ;
if (ev != NULL) ev->unpark() ;
}
通過上面的程式碼分析可以知道,了Thread.interrupt()方法實際就是設定一箇中斷狀態標識為真,並且通過ParkEvent的取消駐留方法來喚醒執行緒。
- 對於同步阻塞的執行緒,被喚醒以後會繼續嘗試獲取鎖,如果失敗仍然可能被公園
- 在呼叫ParkEvent的公園方法之前,會先判斷執行緒的中斷狀態,如果為真,會清除當前執行緒的中斷標識
- 的Object.wait,了Thread.sleep,的Thread.join會丟擲InterruptedException的
這裡給大家普及一個知識點,為什麼的Object.wait,的Thread.sleep和的Thread.join都會丟擲InterruptedException的?首先,這個異常的意思是表示一個阻塞被其他執行緒中斷了。然後,由於執行緒呼叫了中斷()中斷方法,那麼的Object.wait,的Thread.sleep等被阻塞的執行緒被喚醒以後會通過is_interrupted方法判斷中斷標識的狀態變化,如果發現中斷標識為真,則先清除中斷標識,然後丟擲InterruptedException的
需要注意的是,InterruptedException的異常的丟擲並不意味著執行緒必須終止,而是提醒當前執行緒有中斷的操作發生,至於接下來怎麼處理取決於執行緒本身,比如
- 直接捕獲異常不做任何處理
- 將異常往外丟擲
- 停止當前執行緒,並列印異常資訊
- 關注我的技術公眾號【架構師修煉寶典】一週出產1-2篇技術文章。
- Q裙725219329我在分享併發程式設計,分散式,微服務架構,效能優化,原始碼,設計模式,高併發,高可用,春,Netty中,Tomcat時,JVM等技術視訊。
為了讓大家能夠更好的理解上面這段話,我們以了Thread.sleep為例直接從JDK的原始碼中找到中斷標識的清除以及異常丟擲的方法程式碼
找到is_interrupted()方法,linux平臺中的實現在os_linux.cpp檔案中,程式碼如下
bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
assert(Thread::current() == thread || Threads_lock->owned_by_self(),
"possibility of dangling Thread pointer");
OSThread* osthread = thread->osthread();
bool interrupted = osthread->interrupted(); //獲取執行緒的中斷標識
if (interrupted && clear_interrupted) {//如果中斷標識為true
osthread->set_interrupted(false);//設定中斷標識為false
// consider thread->_SleepEvent->reset() ... optional optimization
}
return interrupted;
}
找到了Thread.sleep這個操作在JDK中的原始碼體現,怎麼找?相信如果前面大家有認真看的話,應該能很快找到,程式碼在jvm.cpp檔案中
JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
JVMWrapper("JVM_Sleep");
if (millis < 0) {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
}
//判斷並清除執行緒中斷狀態,如果中斷狀態為true,丟擲中斷異常
if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
}
// Save current thread state and restore it at the end of this block.
// And set new thread state to SLEEPING.
JavaThreadSleepState jtss(thread);
...
注意上面加了中文註釋的地方的程式碼,先判斷is_interrupted的狀態,然後丟擲一個InterruptedException的異常。到此為止,我們就已經分析清楚了中斷的整個流程。
Java的執行緒的中斷標識判斷
瞭解了執行緒。中斷方法的作用以後,再回過頭來看Java中Thread.currentThread()。isInterrupted()這段程式碼,就很好理解了。由於前者先設定了一箇中斷標識為真,所以isInterrupted( )這個方法的返回值為真,故而不滿足而迴圈的判斷條件導致退出迴圈。
這裡有必要再提一句,就是這個執行緒中斷標識有兩種方式復位,第一種是前面提到過的InterruptedException的;另一種是通過Thread.interrupted()對當前執行緒的中斷標識進行復位。