classThreadBlockInVM : public ThreadStateTransition { public: ThreadBlockInVM(JavaThread *thread) : ThreadStateTransition(thread) { // Once we are blocked vm expects stack to be walkable thread->frame_anchor()->make_walkable(thread); //把线程由运行状态转成阻塞状态 trans_and_fence(_thread_in_vm, _thread_blocked); } ... }; 复制代码
//这个枚举是用来追踪线程在代码的那一块执行,用来给 safepoint code使用,有4种重要的类型,_thread_new/_thread_in_native/_thread_in_vm/_thread_in_Java。形如xxx_trans的状态都是中间状态,表示线程正在由一种状态变成另一种状态,这种方式使得 safepoint code在处理线程状态时,不需要对线程进行挂起,使得safe point code运行更快,而给定一个状态,通过+1就可以得到他的转换状态 enumJavaThreadState { _thread_uninitialized = 0, // should never happen (missing initialization) _thread_new = 2, // just starting up, i.e., in process of being initialized _thread_new_trans = 3, // corresponding transition state (not used, included for completeness) _thread_in_native = 4, // running in native code . This is a safepoint region, since all oops will be in jobject handles _thread_in_native_trans = 5, // corresponding transition state _thread_in_vm = 6, // running in VM _thread_in_vm_trans = 7, // corresponding transition state _thread_in_Java = 8, // Executing either interpreted or compiled Java code running in Java or in stub code _thread_in_Java_trans = 9, // corresponding transition state (not used, included for completeness) _thread_blocked = 10, // blocked in vm _thread_blocked_trans = 11, // corresponding transition state _thread_max_state = 12// maximum thread state+1 - used for statistics allocation }; 复制代码
voidtrans_and_fence(JavaThreadState from, JavaThreadState to){ transition_and_fence(_thread, from, to);} //_thread即构造函数传进来de thread // transition_and_fence must be used on any thread state transition // where there might not be a Java call stub on the stack, in // particular on Windows where the Structured Exception Handler is // set up in the call stub. os::write_memory_serialize_page() can // fault and we can't recover from it on Windows without a SEH in // place. //transition_and_fence方法必须在任何线程状态转换的时候使用 staticinlinevoidtransition_and_fence(JavaThread *thread, JavaThreadState from, JavaThreadState to){ assert(thread->thread_state() == from, "coming from wrong thread state"); assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states"); //标识线程转换中 thread->set_thread_state((JavaThreadState)(from + 1));
// 设置内存屏障,确保新的状态能够被VM 线程看到 if (os::is_MP()) { if (UseMembar) { // Force a fence between the write above and read below OrderAccess::fence(); } else { // Must use this rather than serialization page in particular on Windows InterfaceSupport::serialize_memory(thread); } }
if (SafepointSynchronize::do_call_back()) { SafepointSynchronize::block(thread); } //线程状态转换成最终的状态,对待这里的场景就是阻塞 thread->set_thread_state(to);
enumThreadState { ALLOCATED, // Memory has been allocated but not initialized INITIALIZED, // The thread has been initialized but yet started RUNNABLE, // Has been started and is runnable, but not necessarily running MONITOR_WAIT, // Waiting on a contended monitor lock CONDVAR_WAIT, // Waiting on a condition variable OBJECT_WAIT, // Waiting on an Object.wait() call BREAKPOINTED, // Suspended at breakpoint SLEEPING, // Thread.sleep() ZOMBIE // All done, but not reclaimed yet }; 复制代码