平時屬于用的比較多的一個Thread類 按照定義集成了Thread類 又封裝了Handler類
以下是HandlerThread的用法
// 步驟1:創(chuàng)建HandlerThread實例對象
// 傳入?yún)?shù) = 線程名字,作用 = 標(biāo)記該線程
? HandlerThread mHandlerThread = new HandlerThread("handlerThread");
// 步驟2:啟動線程
? mHandlerThread.start();
// 步驟3:創(chuàng)建工作線程Handler & 復(fù)寫handleMessage()
// 作用:關(guān)聯(lián)HandlerThread的Looper對象、實現(xiàn)消息處理操作 & 與其他線程進(jìn)行通信
// 注:消息處理操作(HandlerMessage())的執(zhí)行線程 = mHandlerThread所創(chuàng)建的工作線程中執(zhí)行
? Handler workHandler = new Handler( handlerThread.getLooper() ) {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean handleMessage(Message msg) {
? ? ? ? ? ? ? ? ...//消息處理
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? });
————————————————
原文鏈接:https://blog.csdn.net/carson_ho/article/details/79285760
詳細(xì)使用可以參考原文鏈接??
其實我自己通過查看源碼 (一百來行代碼)
個人覺得HanderThread 的作用 比較通俗易懂的理解就是 這個Thread不是用來跑run方法的,
而是這個Thread是用來創(chuàng)建Looper對象的,如果類比主線程和子線程是電梯(模式固定),那么HanderThread 就是個移動的梯子,可以主線程和線程 也可以線程和線程,這么理解HanderThread會比定義會更通俗易懂點。