从源码与官方文档看之Handler篇(五)

简介: 尽管前路坎坷,可我们还是继续走了下去,一转眼已经又到了第五篇了,每篇内容较少,所以后面整理完时,会汇总成一篇文章,相信这样做会更具有可读性。

正篇

首先我们继续看代码:

/**
 * Create a new Handler whose posted messages and runnables are not subject to
 * synchronization barriers such as display vsync.
 *
 * <p>Messages sent to an async handler are guaranteed to be ordered with respect to one another,
 * but not necessarily with respect to messages from other Handlers.</p>
 *
 * @see #createAsync(Looper, Callback) to create an async Handler with custom message handling.
 *
 * @param looper the Looper that the new Handler should be bound to
 * @return a new async Handler instance
 */
@NonNull
public static Handler createAsync(@NonNull Looper looper) {
    if (looper == null) throw new NullPointerException("looper must not be null");
    return new Handler(looper, null, true);
}

从方法名可以看出,这是用来创建异步的Handler的方法,方法内容可看出这是这个方法能对Looper线程判空。注释的大意如下:

创建一个新的 Handler,其发布的消息和可运行对象不受同步障碍(例如显示 vsync(垂直同步))的影响。 保证发送到异步处理程序的消息是相对于彼此排序的,但不一定相对于来自其他处理程序的消息。

参数: looper – 新 Handler 应该绑定到的 Looper 回报: 一个新的异步处理程序实例 也可以看看: 使用自定义消息处理创建异步处理程序。

接着我们看下一个方法:

/**
 * Create a new Handler whose posted messages and runnables are not subject to
 * synchronization barriers such as display vsync.
 *
 * <p>Messages sent to an async handler are guaranteed to be ordered with respect to one another,
 * but not necessarily with respect to messages from other Handlers.</p>
 *
 * @see #createAsync(Looper) to create an async Handler without custom message handling.
 *
 * @param looper the Looper that the new Handler should be bound to
 * @return a new async Handler instance
 */
@NonNull
public static Handler createAsync(@NonNull Looper looper, @NonNull Callback callback) {
    if (looper == null) throw new NullPointerException("looper must not be null");
    if (callback == null) throw new NullPointerException("callback must not be null");
    return new Handler(looper, callback, true);
}

注释大意:

创建一个新的 Handler,其发布的消息和可运行对象不受同步障碍(例如显示 vsync)的影响。

保证发送到异步处理程序的消息是相对于彼此排序的,但不一定相对于来自其他Handler的消息。

参数:

looper – 新创建的Handler 应该绑定到的 Looper线程

返回:

一个新的异步处理程序实例 也可以看看: 创建一个没有自定义消息处理的异步处理程序。 (未完待续)

相关文章
|
消息中间件 Android开发
Handler源码解读——handler使用时的注意事项
工作中经常会遇到从子线程发送消息给主线程,让主线程更新UI的操作,常见的有handler.sendMessage(Message),和handler.post(runnable)和handler.postDelayed(runnable, milliseconds);一直在使用这些方法,却不知道他们的原理,今天就来解释一下他们的原理。
从源码与官方文档看之Handler篇(七)
接下来就是我们可以正常调用的Handler类方法了,希望早日完成Handler类的源码阅读。
|
Android开发
从源码与官方文档看之Handler篇(八)
后面更文节奏可以舒缓一些了,每篇文章的篇幅也会长些许。 废话不多说,我们继续看Handler类。
|
安全 API
从源码与官方文档看之Handler篇(六)
今天继续看Handler类,构造方法已经基本全部看完,后面应该是一些类的一般方法之类的内容了,继续加油。
|
消息中间件 Android开发
从源码与官方文档看之Handler篇(九)
今天我们来详细看看Handler的post()方法吧
|
Java Android开发
从源码与官方文档看之Handler篇(二)
今天又发现自己原来是Handler这个类名打错了导致前面几篇一直再说Java的Handle,而安卓的是Handler,前一篇文章我们主要翻译了安卓官方对Handler的注释,这一篇我们来看看Handler类的一些属性,成员变量。
|
安全 API 调度
从源码与官方文档看之Handler篇(十)
每次阅读源码,我都想着许多过往云烟,可以说,一切不在一样,不过,想再多又如何,倒不如按下心思,在手中记下这一切更为妥当。还是看看这些充满智慧结晶的源码吧!
|
安全
从源码与官方文档看之Handler篇(四)
上回我们说到第一个Handler类的含有控制是否异步执行的构造函数,今天我们继续往下阅读,相信能更清楚的了解Handler类的机制。
从源码与官方文档看之Handler篇(三)
前面我们说到dispatchMessage方法,今天我们继续往下看源码。
105 0
|
消息中间件 Java 调度
从源码与官方文档看之Handler篇(一)
好家伙,写了四篇文章才发现自己看源码的Handle所属的包不一样