2010-03-08 54 views

回答

4

那麼,你也可以看看這裏http://openjdk.java.net。 For ver。 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 

0

如果您的意思是「內部如何實現Thread.sleep()」,那麼這是一個本地方法,這意味着它依賴於平臺和JVM實現。我希望大多數實現依賴於底層操作系統提供的線程機制。

+0

Thanks Ash,但本機代碼中使用的任何編碼邏輯。 – 2010-03-08 07:06:31

+0

這是一個問題嗎?如果是這是什麼意思? – EJP 2010-03-08 09:47:15

1

在您看來問的意義上,「邏輯背後」Thread.sleep()將在操作系統內核或系統庫中實現。

因此,「邏輯」可能因操作系統而異。對於Linux,BSD或OpenSolaris等開放源代碼操作系統,您可以深入研究OS源代碼來弄清楚。對於封閉的源操作系統,您可能需要採取逆向工程。不管怎樣,實現「邏輯」(例如,是否使用定時器)可能不同於操作系統,操作系統和操作系統版本,也可能不同。

+0

感謝斯蒂芬的指導。 – 2010-03-08 07:32:11