通过Java源代码我们有
// variable not written or updated anywhere on this class
private volatile int threadStatus = 0;
public State getState() {
// get current thread state
return sun.misc.VM.toThreadState(threadStatus);
}
Run Code Online (Sandbox Code Playgroud)
如何以及在何处threadStatus更新?
我们的想法是最终尝试使用AOP编写更新方法,并对threadStatus更改进行回调.
在OpenJDK文件中的源代码中,hotspot/src/share/vm/classfile/javaClasses.cpp您可以看到以下代码:
// Write the thread status value to threadStatus field in java.lang.Thread java class.
void java_lang_Thread::set_thread_status(oop java_thread,
java_lang_Thread::ThreadStatus status) {
// The threadStatus is only present starting in 1.5
if (_thread_status_offset > 0) {
java_thread->int_field_put(_thread_status_offset, status);
}
}
Run Code Online (Sandbox Code Playgroud)
看起来状态是用本机代码管理的.这意味着您无法从java代码中拦截其更改.