为什么在Android中使用HandlerThread [英] Why use HandlerThread in Android

查看:128
本文介绍了为什么在Android中使用HandlerThread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android中,如果我不使用HandlerThread(将其Looper传递给Handler),则可以使用Handler来发布/处理消息,这是否意味着在这种情况下Handler使用MainThread(UI线程)的Looper?

In android , Handler can be used to post / handle message, if I don't use a HandlerThread (pass its Looper to Handler), does that mean in this case Handler use MainThread (UI Thread) 's Looper ?

如果Handler使用MainThread的Looper将会得到什么结果?可能导致mainThread被阻止?

What result will get if Handler uses MainThread's Looper ? May cause mainThread blocked ?

推荐答案

如果您想一次执行一个后台任务,并且希望这些任务按执行顺序运行,则可以使用HandlerThread.

You would use HandlerThread in case that you want to perform background tasks one at a time and you want that those tasks will run at the order of execution.

例如,如果您要一个接一个地进行多个网络后台操作.

For example if you want to make several network background operations one by one.

是的,HandlerThread有它自己的循环程序,可以创建并发布处理程序(这样就不会阻塞主线程).

Yes, the HandlerThread has it's own looper and handlers could be created and post it, (so it would not block the main thread).

这篇关于为什么在Android中使用HandlerThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆