1、基礎使用
public class DemoActivity extends AppCompatActivity {
private static final String TAG = "DemoActivity";
private WorkHandler workHandler;
private Button sendMsgDiffBt;
@Override
protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
HandlerThread workThread = new HandlerThread("SessionManager$1");
workThread.start();
workHandler = new WorkHandler(workThread.getLooper(),this);
sendMsgDiffBt = findViewById(R.id.bt_send_msg_diff);
sendMsgDiffBt.setOnClickListener(view -> {
workHandler.sendEmptyMessage(1);
});
}
static class WorkHandler extends Handler {
private WeakReference<AppCompatActivity> activityWeakReference = null;
public WorkHandler(Looper looper, AppCompatActivity appCompatActivity) {
super(looper);
activityWeakReference = new WeakReference<>(appCompatActivity);
}
@Override
public void handleMessage(Message msg) {
int what = msg.what;
Log.i(TAG, "handleMessage: " + Looper.getMainLooper().getThread().getName());
Log.i(TAG, "handleMessage: " + Looper.myLooper().getThread().getName());
Log.i(TAG, "thread:" + Thread.currentThread().getId() + "," + Thread.currentThread().getName());
}
}
}
以上是一個Demo標準的寫法,沒有業務邏輯,我們來看一一下他的輸出

根據控制臺的輸出handleMessage的訊息我們可以看出來,,主執行緒是main執行緒,而當前執行緒是我們自定義名字的執行緒,所以我們可以去猜測他就是一個執行緒,由于沒有證據去支持這個猜測,我們需要去看原始碼,去證實這個猜測;
2、原始碼決議
原始碼不多我們就將他的所有都展示出來,注釋我就轉換成中文了,方便我們我們去決議它了
package android.os;
import android.annotation.NonNull;
import android.annotation.Nullable;
public class HandlerThread extends Thread {
int mPriority;
int mTid = -1;
Looper mLooper;
private @Nullable
Handler mHandler;
public HandlerThread(String name) {
super(name);
mPriority = Process.THREAD_PRIORITY_DEFAULT;
}
/**
* Constructs a HandlerThread.
* @param name 名稱
* @param priority 優先級 越大優先級越高
* {@link android.os.Process} and not from java.lang.Thread.
*/
public HandlerThread(String name, int priority) {
super(name);
mPriority = priority;
}
/**
* 可以用來處理主執行緒的業務
*/
protected void onLooperPrepared() {
}
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
/**
* This method returns the Looper associated with this thread. If this thread not been started
* or for any reason isAlive() returns false, this method will return null. If this thread
* has been started, this method will block until the looper has been initialized.
* @return The looper.
*/
public Looper getLooper() {
if (!isAlive()) {
return null;
}
// If the thread has been started, wait until the looper has been created.
synchronized (this) {
while (isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
return mLooper;
}
/**
* @return 獲取當前Handler
* @hide
*/
@NonNull
public Handler getThreadHandler() {
if (mHandler == null) {
mHandler = new Handler(getLooper());
}
return mHandler;
}
/**
* 非安全的退出處理程式執行緒的回圈器
* @see #quitSafely
*/
public boolean quit() {
Looper looper = getLooper();
if (looper != null) {
looper.quit();
return true;
}
return false;
}
/**
* 安全的退出處理程式執行緒的回圈器
* @see #quitSafely
*/
public boolean quitSafely() {
Looper looper = getLooper();
if (looper != null) {
looper.quitSafely();
return true;
}
return false;
}
/**
* Returns the identifier of this thread. See Process.myTid().
*/
public int getThreadId() {
return mTid;
}
}
總結一下:從原始碼當中我們首先可以看出來在android包下,他是系統提供,其次可看到HandlerThread 繼承的就是一個Thread ,通過對Looper的操作進行分裝的一個幫助類,如果對Looper不熟悉的,可以去了解一下Handler的訊息機制,這個就不多說這個了,了解其原理那我們是不是也可以封裝一個我們自己的HandlerThread,哈哈,那根據它的業務邏輯我們是不是可以應用到我們的應用場景了,比如單執行緒任務當中,這個看業務需求,不是必須的拉,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343163.html
標籤:其他
下一篇:解決Vue專案在iOS 10 報錯 “Cannot declare a let variable twice: ‘r‘”
