HandlerThread源码分析,android自定义view面试

public HandlerThread(String name, int priority) {

super(name);

mPriority = priority;

}

/**

  • Call back method that can be explicitly overridden if needed to execute some

  • setup before Looper loops.

*/

protected void onLooperPrepared() {

}

@Override

public void run() {

mTid = Process.myTid();

Looper.prepare();

//持有锁机制来获得当前线程的Looper对象

synchronized (this) {

mLooper = Looper.myLooper();

//发出通知,当前线程已经创建mLooper对象成功,这里主要是通知getLooper方法中的wait

notifyAll();

}

//设置线程的优先级别

Process.setThreadPriority(mPriority);

//这里默认是空方法的实现,我们可以重写这个方法来做一些线程开始之前的准备,方便扩展

onLooperPrepared();

Looper.loop();

mTid = -1;

}

public Looper getLooper() {

if (!isAlive()) {

return null;

}

// 直到线程创建完Looper之后才能获得Looper对象,Looper未创建成功,阻塞

synchronized (this) {

while (isAlive() && mLooper == null) {

try {

wait();

} catch (InterruptedException e) {

}

}

}

return mLooper;

}

public boolean quit() {

Looper looper = getLooper();

if (looper != null) {

looper.quit();

return true;

}

return false;

}

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;

}

}

1)首先我们先来看一下它的构造方法

public HandlerThread(String name) {

super(name);

mPriority = Process.THREAD_PRIORITY_DEFAULT;

}

public HandlerThread(String name, int priority) {

super(name);

mPriority = priority;

}

有两个构造方法,一个参数的和两个参数的,name代表当前线程的名称,priority为线程的优先级别

2)接着我们来看一下run()方法,在run方法里面我们可以看到我们会初始化一个Looper,并设置线程的优先级别

public void run() {

mTid = Process.myTid();

Looper.prepare();

//持有锁机制来获得当前线程的Looper对象

synchronized (this) {

mLooper = Looper.myLooper();

//发出通知,当前线程已经创建mLooper对象成功,这里主要是通知getLooper方法中的wait

notifyAll();

}

//设置线程的优先级别

Process.setThreadPriority(mPriority);

//这里默认是空方法的实现,我们可以重写这个方法来做一些线程开始之前的准备,方便扩展

onLooperPrepared();

Looper.loop();

mTid = -1;

}

  • 还记得我们前面我们说到使用HandlerThread的时候必须笑调用start()方法,接着才可以将我们的HandlerThread和我们的handler绑定在一起吗?其实原因就是我们是在run()方法才开始初始化我们的looper,而我们调用HandlerThread的start()方法的时候,线程会交给虚拟机调度,由虚拟机自动调用run方法

mHandlerThread.start();

mThreadHandler = new Handler(mHandlerThread.getLooper()) {

@Override

public void handleMessage(Message msg) {

checkForUpdate();

if(isUpdate){

mThreadHandler.sendEmptyMessage(MSG_UPDATE_INFO);

}

}

};

  • 这里我们为什么要使用锁机制和notifyAll();,原因我们可以从getLooper()方法中知道

public Looper getLooper() {

if (!isAlive()) {

return null;

}

// 直到线程创建完Looper之后才能获得Looper对象,Looper未创建成功,阻塞

synchronized (this) {

while (isAlive() && mLooper == null) {

try {

wait();

} catch (InterruptedException e) {

}

}

}

return mLooper;

}

总结:在获得mLooper对象的时候存在一个同步的问题,只有当线程创建成功并且Looper对象也创建成功之后才能获得mLooper的值。这里等待方法wait和run方法中的notifyAll方法共同完成同步问题。

3)接着我们来看一下quit方法和quitSafe方法

//调用这个方法退出Looper消息循环,及退出线程

public boolean quit() {

Looper looper = getLooper();

if (looper != null) {

looper.quit();

return true;

}

return false;

}

//调用这个方法安全地退出线程

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)

public boolean quitSafely() {

Looper looper = getLooper();

if (looper != null) {

looper.quitSafely();

return true;

}

return false;

}

跟踪这两个方法容易知道只两个方法最终都会调用MessageQueue的queue(boolean safe)方法

void quit(boolean safe) {

if (!mQuitAllowed) {

throw new IllegalStateException(“Main thread not allowed to quit.”);

}

synchronized (this) {

if (mQuitting) {

return;

}

mQuitting = true;

//安全退出调用这个方法

if (safe) {

removeAllFutureMessagesLocked();

} else {//不安全退出调用这个方法

removeAllMessagesLocked();

}

// We can assume mPtr != 0 because mQuitting was previously false.

nativeWake(mPtr);

}

}

不安全的会调用removeAllMessagesLocked();这个方法,我们来看这个方法是怎样处理的,其实就是遍历Message链表,移除所有信息的回调,并重置为null

private void removeAllMessagesLocked() {

Message p = mMessages;

while (p != null) {

Message n = p.next;

总结

最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

上述【高清技术脑图】以及【配套的架构技术PDF】可以关注我免费获取

Android学习PDF+架构视频+面试文档+源码笔记

,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

[外链图片转存中…(img-mC5AOZhm-1646395530445)]

[外链图片转存中…(img-AEKcfWnf-1646395530447)]

上述【高清技术脑图】以及【配套的架构技术PDF】可以关注我免费获取

Android学习PDF+架构视频+面试文档+源码笔记

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值