美文网首页
HandlerThread

HandlerThread

作者: 4ff5e1e6459f | 来源:发表于2019-06-07 20:59 被阅读0次

HanlderThread 是一个线程,在 start 之后会创建一个本线程所有的 Looper (包含一个消息队列)。

通过它的 getLooper() 获取这个 Looper 并用来构建一个 Handler。

HandlerThread ht =new HandlerThread("ht");
ht.start();
Handler handler =new Handler(ht.getLooper());

HandlerThread 适合处理耗时的串行任务,比 SingleThreadPool 线程池更轻量。

注意:
1、在构造 Handler 时要防止出现内存泄漏
2、HandlerThread 启动后需要手动调用 quit() 或 quitSafely() 方法关闭

相关文章

网友评论

      本文标题:HandlerThread

      本文链接:https://www.haomeiwen.com/subject/ctvvxctx.html