成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

從 Hotspot 虛擬機(jī)角度來分析 Java 線程啟動(dòng)

開發(fā) 后端
Java 線程其實(shí)是映射到操作系統(tǒng)的內(nèi)核線程上的,所以 Java 線程基本上也就是操作系統(tǒng)在進(jìn)行管理。在 Linux系統(tǒng)中,線程和進(jìn)程用的是同一個(gè)結(jié)構(gòu)體進(jìn)行描述的,只不過進(jìn)程擁有自己獨(dú)立的地址空間,而同一個(gè)進(jìn)程的多個(gè)線程之間是共享資源的。

[[422986]]

基本概念

Java 線程其實(shí)是映射到操作系統(tǒng)的內(nèi)核線程上的,所以 Java 線程基本上也就是操作系統(tǒng)在進(jìn)行管理。在 Linux系統(tǒng)中,線程和進(jìn)程用的是同一個(gè)結(jié)構(gòu)體進(jìn)行描述的,只不過進(jìn)程擁有自己獨(dú)立的地址空間,而同一個(gè)進(jìn)程的多個(gè)線程之間是共享資源的。

簡單說明:本文基于 openjdk 1.8 進(jìn)行

線程狀態(tài)

每種線程狀態(tài)的切換條件, 以及調(diào)用方法如下圖所示 :

線程具有以下幾種狀態(tài) Java 的線程狀態(tài)在 Thread.State 枚舉中定義代碼如下

  1. public enum State { 
  2.     //新創(chuàng)建,未啟動(dòng) 
  3.     NEW, 
  4.  
  5.     //在jvm 中運(yùn)行,也可能正在等待操作系統(tǒng)的其他資源 
  6.     RUNNABLE, 
  7.  
  8.     //阻塞,并且正在等待監(jiān)視器鎖 
  9.     BLOCKED, 
  10.  
  11.     //處于等待狀態(tài)的線程,正在等待另一個(gè)線程執(zhí)行特定的操作 
  12.     WAITING, 
  13.  
  14.     //限期等待, 可以設(shè)置最大等待時(shí)間 
  15.     TIMED_WAITING, 
  16.  
  17.     //結(jié)束 
  18.     TERMINATED; 

線程創(chuàng)建

繼承 Thread 類, 代碼如下:

  1. class PrimeThread extends Thread { 
  2.     long minPrime; 
  3.     PrimeThread(long minPrime) { 
  4.         this.minPrime = minPrime; 
  5.     } 
  6.  
  7.     public void run() { 
  8.         // compute primes larger than minPrime 
  9.         . . . 
  10.         } 
  11.  
  12. // 啟動(dòng)線程 
  13. PrimeThread p = new PrimeThread(143); 
  14. p.start(); 

實(shí)現(xiàn) Runable 接口, 代碼如下 (通常推薦使用這種方式):

  1. class PrimeRun implements Runnable { 
  2.     long minPrime; 
  3.     PrimeRun(long minPrime) { 
  4.         this.minPrime = minPrime; 
  5.     } 
  6.  
  7.     public void run() { 
  8.         // compute primes larger than minPrime 
  9.         . . . 
  10.         } 
  11.  
  12. // 啟動(dòng)線程 
  13. PrimeRun p = new PrimeRun(143); 
  14. new Thread(p).start(); 

hotspot 源碼

JNI 機(jī)制

JNI 是 Java Native Interface 的縮寫,它提供了若干的 API 實(shí)現(xiàn)了Java和其他語言的通信(主要是C和C++)。

JNI的適用場(chǎng)景 當(dāng)我們有一些舊的庫,已經(jīng)使用C語言編寫好了,如果要移植到Java上來,非常浪費(fèi)時(shí)間,而JNI可以支持Java程序與C語言編寫的庫進(jìn)行交互,這樣就不必要進(jìn)行移植了。或者是與硬件、操作系統(tǒng)進(jìn)行交互、提高程序的性能等,都可以使用JNI。需要注意的一點(diǎn)是需要保證本地代碼能工作在任何Java虛擬機(jī)環(huán)境。

  • JNI的副作用 一旦使用JNI,Java程序?qū)G失了Java平臺(tái)的兩個(gè)優(yōu)點(diǎn):
  • 程序不再跨平臺(tái),要想跨平臺(tái),必須在不同的系統(tǒng)環(huán)境下程序編譯配置本地語言部分。

程序不再是絕對(duì)安全的,本地代碼的使用不當(dāng)可能會(huì)導(dǎo)致整個(gè)程序崩潰。一個(gè)通用規(guī)則是,調(diào)用本地方法應(yīng)該集中在少數(shù)的幾個(gè)類當(dāng)中,這樣就降低了Java和其他語言之間的耦合。

舉個(gè)例子 這塊操作比較多,可以參考如下的資料

  1. https://www.runoob.com/w3cnote/jni-getting-started-tutorials.html 

啟動(dòng)流程

啟動(dòng)流程如下

線程啟動(dòng)

Java 創(chuàng)建線程 Thread 實(shí)例之后,是通過 start 方法進(jìn)行啟動(dòng)該線程,通知執(zhí)行。在 start 方法的內(nèi)部,調(diào)用的是 start0() 這個(gè)本地方法。我們可以從該方法為入口分析 JVM 對(duì)于 Thread 的底層實(shí)現(xiàn)。

  1. public synchronized void start() { 
  2.     // 判斷線程狀態(tài) 
  3.     if (threadStatus != 0) 
  4.         throw new IllegalThreadStateException(); 
  5.  
  6.     // 添加到組 
  7.     group.add(this); 
  8.  
  9.     boolean started = false
  10.     try { 
  11.         // 啟動(dòng)線程 
  12.         start0(); 
  13.         started = true
  14.     } finally { 
  15.         try { 
  16.             if (!started) { 
  17.                 group.threadStartFailed(this); 
  18.             } 
  19.         } catch (Throwable ignore) { 
  20.             /* do nothing. If start0 threw a Throwable then 
  21.               it will be passed up the call stack */ 
  22.         } 
  23.     } 
  24.  
  25. private native void start0(); 

start0() 是一個(gè)本地方法,咱們按照 JNI 規(guī)范可以到 hotspot 虛擬源碼中查找 java_lang_Thread_start0 這個(gè)函數(shù)。定義如下:

  1. /* 
  2.  * Class:     java_lang_Thread 
  3.  * Method:    start0 
  4.  * Signature: ()V 
  5.  */ 
  6. JNIEXPORT void JNICALL Java_java_lang_Thread_start0 
  7.   (JNIEnv *, jobject); 

通過注釋 Method: start0 我可以猜到,在 jvm 的內(nèi)部也可能會(huì)存在 start0 這個(gè)方法,于是我又搜索了一下這個(gè)方法,找到了 Thread.c 文件。可以看到里面有一個(gè) Java_java_lang_Thread_registerNatives() 方法,這就是用來初始化在 Thread.java 與其他方法的綁定,并且在 Threa.java 的第一個(gè) static 塊中就調(diào)用了這個(gè)方法,保證這個(gè)方法在類加載中是第一個(gè)被調(diào)用的方法。這個(gè) native 方法的作用是為其他 native 方法注冊(cè)到JVM中。代碼如下所示:

  1. static JNINativeMethod methods[] = { 
  2.     {"start0",           "()V",        (void *)&JVM_StartThread}, 
  3.     {"stop0",            "(" OBJ ")V", (void *)&JVM_StopThread}, 
  4.     {"isAlive",          "()Z",        (void *)&JVM_IsThreadAlive}, 
  5.     {"suspend0",         "()V",        (void *)&JVM_SuspendThread}, 
  6.     {"resume0",          "()V",        (void *)&JVM_ResumeThread}, 
  7.     {"setPriority0",     "(I)V",       (void *)&JVM_SetThreadPriority}, 
  8.     {"yield",            "()V",        (void *)&JVM_Yield}, 
  9.     {"sleep",            "(J)V",       (void *)&JVM_Sleep}, 
  10.     {"currentThread",    "()" THD,     (void *)&JVM_CurrentThread}, 
  11.     {"countStackFrames""()I",        (void *)&JVM_CountStackFrames}, 
  12.     {"interrupt0",       "()V",        (void *)&JVM_Interrupt}, 
  13.     {"isInterrupted",    "(Z)Z",       (void *)&JVM_IsInterrupted}, 
  14.     {"holdsLock",        "(" OBJ ")Z", (void *)&JVM_HoldsLock}, 
  15.     {"getThreads",        "()[" THD,   (void *)&JVM_GetAllThreads}, 
  16.     {"dumpThreads",      "([" THD ")[[" STE, (void *)&JVM_DumpThreads}, 
  17.     {"setNativeName",    "(" STR ")V", (void *)&JVM_SetNativeThreadName}, 
  18. }; 
  19.  
  20. #undef THD 
  21. #undef OBJ 
  22. #undef STE 
  23. #undef STR 
  24.  
  25. JNIEXPORT void JNICALL 
  26. Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass cls) 
  27.     (*env)->RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods)); 

再回到我們的 start0 方法,此時(shí)我們就去查找 JVM_StartThread 方法是在他是在/hotspot/src/share/vm/prims/jvm.cpp 這個(gè)文件里面:

  1. JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread)) 
  2.   JVMWrapper("JVM_StartThread"); 
  3.   JavaThread *native_thread = NULL
  4.  
  5.   // We cannot hold the Threads_lock when we throw an exception, 
  6.   // due to rank ordering issues. Example:  we might need to grab the 
  7.   // Heap_lock while we construct the exception. 
  8.   bool throw_illegal_thread_state = false
  9.  
  10.   // We must release the Threads_lock before we can post a jvmti event 
  11.   // in Thread::start. 
  12.   { 
  13.     // Ensure that the C++ Thread and OSThread structures aren't freed before 
  14.     // we operate. 
  15.     MutexLocker mu(Threads_lock); 
  16.  
  17.     // 1. 判斷 Java 線程是否啟動(dòng),如果已經(jīng)啟動(dòng),拋出異常 
  18.     if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) { 
  19.       throw_illegal_thread_state = true
  20.     } else { 
  21.       // 2. 如果沒有創(chuàng)建,則會(huì)創(chuàng)建線程  
  22.       jlong size = 
  23.              java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));   
  24.       size_t sz = size > 0 ? (size_t) size : 0; 
  25.       // 虛擬機(jī)創(chuàng)建 JavaThread, 該類內(nèi)部會(huì)創(chuàng)建操作系統(tǒng)線程,然后關(guān)聯(lián) Java 線程   
  26.       native_thread = new JavaThread(&thread_entry, sz); 
  27.  
  28.       if (native_thread->osthread() != NULL) { 
  29.         // Note: the current thread is not being used within "prepare"
  30.         native_thread->prepare(jthread); 
  31.       } 
  32.     } 
  33.   } 
  34.  
  35.   if (throw_illegal_thread_state) { 
  36.     THROW(vmSymbols::java_lang_IllegalThreadStateException()); 
  37.   } 
  38.  
  39.   assert(native_thread != NULL"Starting null thread?"); 
  40.  
  41.   if (native_thread->osthread() == NULL) { 
  42.     // No one should hold a reference to the 'native_thread'
  43.     delete native_thread; 
  44.     if (JvmtiExport::should_post_resource_exhausted()) { 
  45.       JvmtiExport::post_resource_exhausted( 
  46.         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS, 
  47.         "unable to create new native thread"); 
  48.     } 
  49.     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), 
  50.               "unable to create new native thread"); 
  51.   } 
  52.  
  53.   // 設(shè)置線程狀態(tài)為 Runnable 
  54.   Thread::start(native_thread); 
  55.  
  56. JVM_END 

JavaThread 類的構(gòu)造方法我們一起來看看,他是通過 os::create_thread 函數(shù)來進(jìn)行創(chuàng)建 Java 對(duì)應(yīng)的內(nèi)核線程

  1. JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) : 
  2.   Thread() 
  3.   if (TraceThreadEvents) { 
  4.     tty->print_cr("creating thread %p", this); 
  5.   } 
  6.   initialize(); 
  7.   _jni_attach_state = _not_attaching_via_jni; 
  8.   set_entry_point(entry_point); 
  9.   os::ThreadType thr_type = os::java_thread; 
  10.   thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : 
  11.                                                      os::java_thread; 
  12.   // 創(chuàng)建Java線程對(duì)應(yīng)的內(nèi)核線 
  13.   os::create_thread(this, thr_type, stack_sz); 
  14.   _safepoint_visible = false

os:create_thread 其實(shí)主要就是一個(gè)用來支持跨平臺(tái)創(chuàng)建線程的, 以 Linux 為例 (hotspot/src/os/linux/vm/os_linux.cpp):

  1. bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) { 
  2.   // ... 
  3.    
  4.   // 創(chuàng)建 OSThread 內(nèi)核線程對(duì)象 
  5.   OSThread* osthread = new OSThread(NULLNULL); 
  6.   // 綁定 
  7.   thread->set_osthread(osthread); 
  8.  
  9.   pthread_t tid; 
  10.   // pthread_create 為 linux api 用來創(chuàng)建線程。 
  11.   int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread); 
  12.   // ...   
  13.   return true

我們可以通過 ubantu 的控制臺(tái)來查詢接口信息

  • man pthread_create 來進(jìn)行查詢文檔

通過文檔我們可以了解,當(dāng) pthread_create 函數(shù)執(zhí)行創(chuàng)建完線程之后會(huì)調(diào)用第三個(gè)參數(shù)傳遞過去的回調(diào)函數(shù)

  • int ret = pthread_create(&tid, &attr, (void* ()(void)) java_start, thread);

在這里就是 java_start 函數(shù)

  1. // Thread start routine for all newly created threads 
  2. static void *java_start(Thread *thread) { 
  3.  
  4.   // 主要是調(diào)用 Thread 的 run 方法 
  5.   thread->run(); 
  6.  
  7.   return 0; 

在 thread.cpp 中 JavaThread::run 方法最終調(diào)用了 thread_main_inner 方法:

  1. // The first routine called by a new Java thread 
  2. void JavaThread::run() { 
  3.   
  4.  
  5.   // We call another function to do the rest so we are sure that the stack addresses used 
  6.   // from there will be lower than the stack base just computed 
  7.   thread_main_inner(); 
  8.  
  9.   // Note, thread is no longer valid at this point! 

在 thread_main_inner 方法內(nèi),在調(diào)用咱們之前創(chuàng)建 JavaThread 對(duì)象的時(shí)候傳遞進(jìn)來的 entry_point 方法:

  1. void JavaThread::thread_main_inner() { 
  2.  
  3.   if (!this->has_pending_exception() && 
  4.       !java_lang_Thread::is_stillborn(this->threadObj())) { 
  5.     { 
  6.       ResourceMark rm(this); 
  7.       this->set_native_thread_name(this->get_thread_name()); 
  8.     } 
  9.     HandleMark hm(this); 
  10.     // 調(diào)用 entry_point 方法 
  11.     this->entry_point()(this, this); 
  12.   } 
  13.  
  14.   DTRACE_THREAD_PROBE(stop, this); 
  15.  
  16.   this->exit(false); 
  17.   delete this; 

通過上面的代碼我們可以看到先創(chuàng)建了一個(gè) JavaThread 對(duì)象, 然后傳入了 thread_entry 方法

  1. // JVM_StartThread 創(chuàng)建操作系統(tǒng)線程,執(zhí)行  thread_entry 函數(shù) 
  2. static void thread_entry(JavaThread* thread, TRAPS) { 
  3.   HandleMark hm(THREAD); 
  4.   Handle obj(THREAD, thread->threadObj()); 
  5.   JavaValue result(T_VOID); 
  6.   // Thrad.start() 調(diào)用 java.lang.Thread 類的 run 方法 
  7.   JavaCalls::call_virtual(&result, 
  8.                           obj, 
  9.                           KlassHandle(THREAD, SystemDictionary::Thread_klass()), 
  10.                           vmSymbols::run_method_name(), 
  11.                           vmSymbols::void_method_signature(), 
  12.                           THREAD); 

我們?cè)賮砜纯次覀?Java 中 Thread 類的 run 方法

  1. public void run() { 
  2.     if (target != null) { 
  3.         // Thread.run() 又調(diào)用 Runnable.run() 
  4.         target.run();  
  5.     } 

參考資料

https://www.jb51.net/article/216231.htm

https://blog.csdn.net/u013928208/article/details/108051796

https://www.cnblogs.com/whhjava/p/9916626.html

https://www.runoob.com/w3cnote/jni-getting-started-tutorials.html

https://developer.51cto.com/art/202011/632936.htm

https://blog.csdn.net/weixin_34384681/article/details/90660510

https://blog.csdn.net/weixin_30267697/article/details/95994035

 

https://zhuanlan.zhihu.com/p/33830504

 

責(zé)任編輯:武曉燕 來源: 運(yùn)維開發(fā)故事
相關(guān)推薦

2020-09-02 07:03:04

虛擬機(jī)HotSpotJava

2010-11-05 09:47:11

OracleJava虛擬機(jī)

2012-08-16 09:07:57

Erlang

2012-08-06 09:26:19

Java虛擬機(jī)垃圾回收

2019-03-19 15:30:42

程序員JVM虛擬機(jī)

2009-06-12 16:15:42

死鎖Java虛擬機(jī)

2025-01-24 00:00:00

JavaHotSpot虛擬機(jī)

2010-02-26 15:28:15

Python虛擬機(jī)

2010-02-22 17:39:22

CentOS vmwa

2017-03-01 20:08:36

PHP內(nèi)核分析

2025-07-04 07:26:58

HotSpot虛擬機(jī)對(duì)象

2012-05-18 10:22:23

2017-11-14 16:43:13

Java虛擬機(jī)線程

2011-04-07 13:40:02

ezjailjail虛擬機(jī)

2013-07-17 09:32:58

2010-07-26 09:02:38

2010-09-17 15:12:57

JVMJava虛擬機(jī)

2020-06-03 19:07:49

Java虛擬機(jī)JVM

2023-06-28 15:53:25

虛擬機(jī)Linux

2019-08-01 08:00:04

AWS虛擬機(jī)Lightsail
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久新 | 青青草原精品99久久精品66 | 婷婷精品 | 精品视频久久久久久 | 国产在线精品一区二区 | 三级黄色片在线播放 | 国产成人免费在线 | 国产精品久久久久久久久免费软件 | 亚洲精品成人 | 日韩精品一区二区三区视频播放 | 亚洲h在线观看 | 日本免费小视频 | 日本一区二区三区四区 | 四虎av电影| 欧美精品福利 | 99精品国产一区二区三区 | 亚洲成人精品 | 一级黄色片在线看 | 国产精品久久久久aaaa樱花 | 色视频网站免费 | 久久99精品国产 | av一区二区在线观看 | 91欧美精品成人综合在线观看 | 男人的天堂在线视频 | 97人人澡人人爽91综合色 | 在线观看国产wwwa级羞羞视频 | 久久久亚洲精品视频 | 国产综合久久久久久鬼色 | 日韩中文字幕在线视频观看 | 日韩精品一区二区三区免费视频 | 男人天堂网址 | 99这里只有精品视频 | 在线国产中文字幕 | 国产中文字幕在线观看 | 日本精品一区二区三区视频 | 国产精品一二区 | 日本亚洲一区 | 免费人成激情视频在线观看冫 | 日本黄色不卡视频 | 一区二区三区四区在线免费观看 | 亚洲精品一区二区在线观看 |