Thread類中sleep是為什麼是靜態方法

2021-07-05 00:03:19 字數 719 閱讀 3652

thread類中sleep是靜態方法,表示當前執行緒休眠。

thread的api

public static native void sleep(long millis) throws interruptedexception;

/**    

* causes the currently executing thread to sleep (temporarily cease

* execution) for the specified number of milliseconds, subject to

* the precision and accuracy of system timers and schedulers. the thread

* does not lose ownership of any monitors.

*thread.sleep是 使 當前 執行得執行緒  休眠,就是 thread.sleep所在**片段的執行緒

1、sleep是靜態方法,那麼在實現runnable的執行緒類也能呼叫。

2、sleep是靜態方法,所以sleep時候只是讓出了cup卻不能釋物件鎖,因為獲取不到物件。???

3、執行緒和例項並不是對等的,不是乙個執行緒是乙個例項,是你建立的例項繼承了thread或者runable,實現了run(),並呼叫start()的時候能執行多個執行緒,例項還是乙個,執行緒卻是多個。所以例項休眠執行緒就休眠了這個假設不成立。

Thread類中的常用方法

1.start 啟動當前執行緒 呼叫當前執行緒的run 2.run 通常需要重寫thread類中的此方法,將建立的執行緒要執行的操作宣告在此方法中 3.currentthread 靜態方法,返回執行當前 的執行緒 4.getname 獲取當前執行緒的名字 5.setname 設定當前執行緒的名字 6...

java中Thread類的join方法

單核cpu執行多執行緒時底層實現原理是多個執行緒間切換,由於cpu的處理速度很快,看上去像多個執行緒同時執行。那麼我們如何實現讓執行緒t1,t2,t3,在t1執行完成後才執行t2,t2執行完成後才執行t3,也就是執行緒的序列化,通過thread類的join方法就可以實現。join方法 將該執行緒加入...

Java中Thread類的方法簡介

thread t1 newthread t1.start 新建執行緒,應該呼叫start 方法啟動執行緒 如果直接呼叫run 方法,該方法也會執行,但會被當做乙個普通的方法,在當前執行緒中順序執行 而如果使用start 方法,則會建立乙個新的執行緒執行run 方法。public void inter...