美文网首页
Handle与HandlerThread简单记录

Handle与HandlerThread简单记录

作者: 梧叶已秋声 | 来源:发表于2019-10-28 16:05 被阅读0次

handler
Message
MessageQueue
Looper

创建主线程时,会自动调用ActivityThread的1个静态的main();而main()内则会调用Looper.prepareMainLooper()为主线程生成1个Looper对象,同时也会生成其对应的MessageQueue对象。

handler.png

Looper
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.

This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.

  class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

从looper在线程中的使用来看,线程与looper handler之间的关系如下。


12.png

但是,实际使用中,上面的那种形式,并没有跟主线程相关联。
实际使用中,还是在主线程中新建一个handler,传递出去。

HandlerThread
HandlerThread就是一个Thread ,A Thread that has a Looper. The Looper can then be used to create Handlers.
其实最早我是先接触HandlerThread,后面才接触的handler的实际使用,工作中反而并没有用到HandlerThread。
具体实现参考Android编程权威指南中的使用,大致关系图如下所示。

12.png

以下代码出自Android编程权威指南,简单省略了。

handler1.post(new Runnable(){
...
mThumbnailDownloadListener.onThumbnailDownloaded(target, bitmap);
};
mThumbnailDownloader.setThumbnailDownloadListener(
new ThumbnailDownloader.ThumbnailDownloadListener<PhotoHolder>() {
  @Override
  public void onThumbnailDownloaded(PhotoHolder photoHolder, Bitmap bitmap) {
    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
    photoHolder.bindDrawable(drawable);
    }
  }
);

这里handler1虽然传递出去了,但是始终跟looper1相关联。

总结:

handler:主线程与子线程通讯的媒介。
Looper:持有MessageQueue,由于调用loop(),不断死循环,作用是给handler发message。
MessageQueue:给handler的message存储在MessageQueue中。

以下出处:一个线程可以有几个Looper?几个Handler?从Looper.prepare()来看看关于Looper的一些问题
https://blog.csdn.net/ly502541243/article/details/87475229#commentsedit
问题:
一个线程可以有几个Looper?
这个问题在刚才已经探讨了,只能有一个,不然调用Looper.prepare()会抛出运行时异常,提示“Only one Looper may be created per thread”
一个线程可以有几个Handler
可以创建无数个Handler,但是他们使用的消息队列都是同一个,也就是同一个Looper

参考链接:
Android Handler:手把手带你深入分析 Handler机制源码
https://www.jianshu.com/p/b4d745c7ff7a

一个线程可以有几个Looper?几个Handler?从Looper.prepare()来看看关于Looper的一些问题
https://blog.csdn.net/ly502541243/article/details/87475229#commentsedit

【Android】源码分析 - Handler消息机制再梳理
https://itimetraveler.github.io/2017/08/03/%E3%80%90Android%E3%80%91%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%20-%20Handler%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6%E5%86%8D%E6%A2%B3%E7%90%86/#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99

Android HandlerThread使用总结
https://waylenw.github.io/Android/android-handler-thread-usage/

HandlerThread详解
https://www.jianshu.com/p/5b6c71a7e8d7

相关文章

网友评论

      本文标题:Handle与HandlerThread简单记录

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