“Causes the currently executing thread to sleep (cease execution)for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any?monitors.”
“Causes the currently executing thread to sleep...”使當前正在執行的線程sleep。我猜想你程序中thread.sleep()是想讓thread線程sleep吧?其實是不可以的,編譯會自動處理成Thread.sleep。而為什么又可以改成WrongWayStopThread.sleep呢?因為WrongWayStopThread繼承自Thread呀,static方法可以被繼承(無法被重寫),classname.method哪里不對呢?
2015-08-10
你可以去API中查下sleep方法的解釋,他是Thread的靜態方法,而靜態方法調用時用類去調用而不是實體對象去調用
2015-09-05
因為如果調用Sleep方法 就只是執行一次
2015-08-24
我的樓層按時間,不按空間- -。
2015-08-24
首先你要知道一個概念:
?main方法本身就是一個線程。
所以該程序有2個線程。
然后你要明白sleep方法是什么意思,老辦法,查API文檔(我推薦大家堅持看英文文檔,英語對程序員至關重要):
“Causes the currently executing thread to sleep...”使當前正在執行的線程sleep。我猜想你程序中thread.sleep()是想讓thread線程sleep吧?其實是不可以的,編譯會自動處理成Thread.sleep。而為什么又可以改成WrongWayStopThread.sleep呢?因為WrongWayStopThread繼承自Thread呀,static方法可以被繼承(無法被重寫),classname.method哪里不對呢?
同時,對這個問題理解了也就理解為什么run方法里不能用Thread.sleep(1000)了。老師也講了doc中的內容,我就不貼了,意思就是:
與sleep()? wait()? jion()相關的方法會clear掉interrupt status,導致while (!this.isInterrupted())的判斷出現并不穩定性()。是不是很官方O__O "…(都這尿性,但嚴謹的說確實是這樣,習慣就好),說簡單但不嚴謹點了就是你調用這些方法就會重置interrupt status,所以這里while中使用Thread.sleep(1000)就會讓while的判斷條件的返回值一直為true。(注意這里返回的native boolean isInterrupted不是上文中的interrupt status,interrupt status是由系統底層決定的,所以上面用clear)
還有我認為如果main中的thread.interrupt()(此方法會直接將native boolean isInterrupted標記為true)剛好在run中的Thread.sleep(1000)之前,while (!this.isInterrupted())之后執行的話,程序會出現bug——正常執行(-__-)b。
以上純屬瞎掰
另外昨晚看到一篇關于程序員學習英語的文章覺得很棒,這里貼過來:
http://blog.jobbole.com/45296
還有嚴重批評1樓不負責任的回答。(╬▔皿▔)╬
加油小伙伴們。?
2015-08-05
Java代碼中存在著大小寫之分,感覺應該不是等同的