为什么在Android中使用HandlerThread [英] Why use HandlerThread in Android
问题描述
在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屋!