gpt4 book ai didi

java.lang.Thread - threadStatus 从何而来?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:21:56 31 4
gpt4 key购买 nike

通过 Java source code我们有那个

// 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);
}

threadStatus 如何以及在哪里更新?

我们的想法是最终尝试使用 AOP 围绕更新方法进行编织,并在 threadStatus 更改时进行回调。

最佳答案

在文件hotspot/src/share/vm/classfile/javaClasses.cpp中的OpenJDK源代码中,您可以看到以下代码:

// 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);
}
}

看起来状态是在 native 代码中管理的。这意味着您无法从 Java 代码中拦截它的更改。

关于java.lang.Thread - threadStatus 从何而来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53406078/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com