j****g 发帖数: 597 | 1 Assuming that an interrupted exception has NOT been thrown and that "
aLiveThread" is a runnable thread, which three guarantee that a thread will
leave the running state?
A yield()
B wait()
C sleep(1000)
D aLiveThread.join()
B and D 肯定是对的。
答案说A是错的,说即使调用yield()不一定会导致该Thread被换出。但我的理解是调用
yield()以后一定会被换出。但是如果没有更高prority的Thread的话,该Thread可能会
再次进入running状态。意思就是肯定会leave running, 但是可能马上就被换入。
答案说C是对的。我的印象是即使调用sleep()也不一定会导致Thread被换出。因为印象
以前在sun工作站上做的时候sleep(250)没有导致当前Thread被换出。
牛人指教一下? | m******t 发帖数: 2416 | 2 I wouldn't sweat on A too much. It's just a matter of whether reading "
leaving the running state" literally. As long as everybody agrees that
yield() does not _always_ cause the thread to effectively yield the
processor, whether it literally leaves the "running" state in the state
diagram really is more theoretical than practical.
IIUC, in any decent JVM implementation, sleep() most certainly would cause a
thread to yeild control, because even if there aren't other application
threads suitable |
|