場景:乙個執行緒繼續執行,需要另乙個執行緒執行完
public
static
void
main
(string[
] args)
catch
(interruptedexception e)
system.out.
println
("當前執行緒"
+thread.
currentthread()
+"結束");
},"執行緒1");
thread thread2 =
newthread((
)->
catch
(interruptedexception e)
system.out.
println
("當前執行緒"
+thread.
currentthread()
+"結束");
},"執行緒2");
thread2.
start()
;}
public
static
void
main
(string[
] args)
throws interruptedexception
catch
(interruptedexception e)
system.out.
println
("當前執行緒"
+thread.
currentthread()
+"結束");
countdownlatch.
countdown()
;},"執行緒1");
thread thread2 =
newthread((
)->
,"執行緒2");
thread.
start()
; thread2.
start()
; countdownlatch.
await()
; system.out.
println
("主線程執行完");
}
主線程和子執行緒
子執行緒通過 handlerthread的thread.getlooper 繫結,在主線程的handler的handlermessage中呼叫threadhandler.sendmessagedelay msg,1000 向子執行緒傳送訊息。在子執行緒中通過handler.sendmessagede...
主線程和子執行緒
主線程負責管理由它建立的子執行緒,建立 啟動 掛起 停止等。主線程通過發訊息的方式管理子執行緒,例如,給子執行緒傳送start 訊息,子執行緒啟動,子執行緒執行入口的run 方法。thread有下面兩個構造方法 thread runnable target,string name thread ru...
在子執行緒中操作主線程
當android啟動activity時,會先啟動乙個主線程 也稱為ui執行緒 用於控制activity的當前view介面,在主線程控制的過程中,子執行緒是不能直接更新主線程資料的,但是android中提供了很多其他執行緒操作ui執行緒的方法 方法一 activity.runonuithread ru...