gpt4 book ai didi

java - java中Thread.sleep()背后的逻辑是什么

转载 作者:行者123 更新时间:2023-12-01 17:39:52 25 4
gpt4 key购买 nike

有人知道java中Thread.sleep背后的逻辑是什么吗? 。他们使用计时器吗

最佳答案

嗯,你也可以看这里http://openjdk.java.net 。对于版本。 7 jvm.cpp中有一些有趣的代码:



JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
JVMWrapper("JVM_Sleep");


if (millis osthread()->get_state();
thread->osthread()->set_state(SLEEPING);
os::sleep(thread, MinSleepInterval, false);
thread->osthread()->set_state(old_state);
}
} else {
ThreadState old_state = thread->osthread()->get_state();
thread->osthread()->set_state(SLEEPING);
if (os::sleep(thread, millis, true) == OS_INTRPT) {
// An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
// us while we were sleeping. We do not overwrite those.
if (!HAS_PENDING_EXCEPTION) {
// TODO-FIXME: THROW_MSG returns which means we will not call set_state()
// to properly restore the thread state. That's likely wrong.
THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
}
}
thread->osthread()->set_state(old_state);
}
JVM_END

关于java - java中Thread.sleep()背后的逻辑是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2399723/

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