JVM内部Thread的实现

这是个棘手的话题,到目前为止,我也没有看得太明白。先记录下我现在的一些理解。
 
先看几个主要的class
Thread、VMThread、JavaThread、OSThread
VMThread和JavaThread都是Thread的子类,而OSThread的父类是CHeapObj。所以,OSThread是一个Helper。
 
VMThread是JVM内部用的Thread,例如GC Thread。
JavaThread则是对应java.lang.Thread
 
通过Thread::set_osthread(OSThread *thread)把OSThread和Thread关联起来。
 
下面看看在Win32下,OSThread是如何与真正的OS的Thread关联起来的。
\hotspot\src\os\win32\vm\os_win32.cpp
os::create_thread方法就是创建操作系统的Thread,并把它和OSThread、Thread关联起来的关键方法。
在这个方法里面,可以看到它调用_beginthreadex创建了操作系统的Thread,并把操作系统中的Thread的Handle与
OSThread关联起来。
 
关于_beginthreadex
MSDN中这样写的:
A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
 
在调用_beginthreadex的时候,第三个参数是一个函数指针,现在指向的是java_start这个函数。
这样所有的Thread创建后,都会去执行java_start这个回调函数。
java_start中又调用了thread->run()方法。
thread->run()的设计应用了设计模式:模板方法。VMThread、JavaThread各自实现了自己的run方法。
 
主线程在哪里?
很显然,main thread在JVM被初始化的时候就已经存在了,只需要在适当的地方把它保存在某个变量中。
回到 os::init()方法
  main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
  if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
                       &main_thread, THREAD_ALL_ACCESS, false, 0)) {
    fatal("DuplicateHandle failed\n");
  }
  main_thread_id = (int) GetCurrentThreadId();
os::init()被调用后,全局变量main_thread, main_thread_id就被赋了正确的值。
 
Threads::create_vm
  |
  |--> Thread::set_as_starting_thread
        |
        |--> os::create_main_thread
              |
              |-->create_os_thread
这是主线程被创建的过程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值