java Thread 片段整理

2021-08-30 18:33:15 字數 765 閱讀 4202

一, 執行緒的生命週期

1. wating state

一旦執行緒被構造, 但是還沒有執行任何**, 那麼它就處於 waiting state,  其他的執行緒可以與之互動, 還可以設定不同的屬性, 如priority, name, daemon status 等.

一旦乙個執行緒 處於 waiting,  它的state 可以被其他的 執行緒 所改變

2. alive state

如果執行緒的start() 方法被呼叫, 則執行緒可能正在執行狀態中,  可以呼叫isalive方法進行判斷

3. terminating thread

在run() 執行完後, 執行緒會結束掉

當然我們可以呼叫system.exit() 方法 強制結束掉程式.

run() 丟擲的unchecked excpetion.

4. stop, suspending, resuming thread

stop, suspend(), resume() 方法已經廢棄掉

sleep() 方法可以暫停當前執行緒的執行

5. thread cleanup

join() 一直柱塞 直到run() 執行完後.

2.4 結束執行緒的兩種方式

1. 設定乙個內部結束的標誌

2. 呼叫interrupt() 方法, 他有兩個效果,

a. 是 導致所有的bocked 方法丟擲interruptedexeption

b. 設定乙個執行緒內部標誌, 標示執行緒已經 interrupted.

java 實用片段整理

1 反射tostring 優勢 簡潔 無須任何配置 model 屬性變化 無需修改tostring 缺陷 安全性問題 反射私有屬性值也會暴露 public static string objecttostring object condition 2 初始化list public static ar...

java thread自學筆記

執行緒有多種寫法。第一種是實現runnable介面。第二種是繼承thread類。第三種是直接寫runnable的實現方法。public class threadtest thread t new thread r t.run output foo t.start output foo try cat...

Java Thread相關(積累)

threadgroup 執行緒組就是由執行緒組成的管理執行緒的類 thread.currentthread getthreadgroup getname currentthread 取得當前執行緒。getthreadgroup 取得當前執行緒所在的組。getname 取得組的名稱。定義乙個執行緒組,...