close

3.jpg

近期有個需求,就是要傳參數給 thread,並且於 thread 執行完後,要取回 thread 的處理結果,除了使用 Handler 之外,還有以下的選擇:

 

line 24~39: 建立一個 runnable class,然後在裡面實作 function,建構一個傳入參數的建構子(line 27~29) 並且把回傳值放到特定變數中 (line 36~38)

line 27~29: 建立 runnable 建構子,讓它可以傳入參數

line 36~38: 建立回傳函數,把回傳值丟出來

 

line 10~21: 建立 runnable 物件,並且傳入參數,然後置入到 thread 中

line 16~17: 啟動thread並且等待它完成

line 19: 取出結果

 

擷取.JPG

Reference:

https://stackoverflow.com/questions/9148899/returning-value-from-thread/9148992

https://matthung0807.blogspot.com/2019/03/java-runnablerun.html

arrow
arrow

    葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()