建立网站的基本流程有哪些步骤,百度开户,浙江建筑诚信平台查询系统,seo是什么缩写应用通信基础架构相关类源码解析
这里主要对Android App开发时#xff0c;常用到的一些通信基础类进行一下源码的简单分析#xff0c;包括#xff1a;
Handler#xff1a;处理器#xff0c;与某个Looper#xff08;一个线程对应一个Looper#xff09;进行关联。用于接…应用通信基础架构相关类源码解析
这里主要对Android App开发时常用到的一些通信基础类进行一下源码的简单分析包括
Handler处理器与某个Looper一个线程对应一个Looper进行关联。用于接收消息并在关联的Looper处理消息。Looper驱动器驱动基于事件的消息系统通信架构的核心其实现在Native层基于epoll机制感兴趣的可自行了解。Runnable: 表示“可执行的代码”本质是Interface规定了Run这个接口。MessageQueue: 消息队列提供了入队、出队等操作。一个线程只能有一个MessageQueue。Thread: 线程类封装了线程相关操作。
基于Android12代码。
类图
Handler
常见用法
private Handler mHandler new Handler(Looper.getMainLooper()) {Overridepublic void handleMessage(Message msg) {// 处理消息}
};private void sendMessage() {// 发送消息Message msg mHandler.obtainMessage();// 填充msgmHandler.sendMessage(msg);
}private void postRunnable() {// 告知Handler一段可执行的代码RunnablemHandler.post(new Runnable() {Overridepublic void run() {// do something}});
}通过上述代码中可以看出。创建Handler时需要绑定Looper也就是绑定到运行的线程上。如过不指定looper使用创建handler时所在线程的Looper。 源码定义在 frameworks/base/core/java/android/os/Handler.java
public Handler() {this(null, false);
}public Handler(NonNull Looper looper) {this(looper, null, false);
}public Handler(Nullable Callback callback, boolean async) {if (FIND_POTENTIAL_LEAKS) {final Class? extends Handler klass getClass();if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) (klass.getModifiers() Modifier.STATIC) 0) {Log.w(TAG, The following Handler class should be static or leaks might occur: klass.getCanonicalName());}}// 获取当前线程对应的LoopermLooper Looper.myLooper();if (mLooper null) {throw new RuntimeException(Cant create handler inside thread Thread.currentThread() that has not called Looper.prepare());}// 使用Looper中的MessageQueuemQueue mLooper.mQueue;mCallback callback;mAsynchronous async;
}UnsupportedAppUsage
public Handler(NonNull Looper looper, Nullable Callback callback, boolean async) {mLooper looper;mQueue looper.mQueue;mCallback callback;mAsynchronous async;
}调用Handler的sendMessage到Handler处理(handleMessage)这个Message。Handler会将这个Message入队到绑定的Looper的MessageQueue(消息队列中)。
public final boolean sendMessage(NonNull Message msg) {// 没有延时 return sendMessageDelayed(msg, 0);
}public final boolean sendMessageDelayed(NonNull Message msg, long delayMillis) {if (delayMillis 0) {delayMillis 0;}return sendMessageAtTime(msg, SystemClock.uptimeMillis() delayMillis);
}public boolean sendMessageAtTime(NonNull Message msg, long uptimeMillis) {MessageQueue queue mQueue;if (queue null) {RuntimeException e new RuntimeException(this sendMessageAtTime() called with no mQueue);Log.w(Looper, e.getMessage(), e);return false;}return enqueueMessage(queue, msg, uptimeMillis);
}private boolean enqueueMessage(NonNull MessageQueue queue, NonNull Message msg,long uptimeMillis) {msg.target this;// 记录一下UIDmsg.workSourceUid ThreadLocalWorkSource.getUid();if (mAsynchronous) {msg.setAsynchronous(true);}// 消息入队MessageQueuereturn queue.enqueueMessage(msg, uptimeMillis);
}Looper从MessageQueue中依次取出Message并告知Handler的handleMessage处理消息想要看懂looper涉及到其Native实现这里不分析可自行了解
Looper
Looper类基于epoll机制提供了一套事件驱动机制。Java层的实现在frameworks/base/core/java/android/os/Looper.java该类中的sMainLooper变量存储了 主线程或者叫UI线程对应的Looper可以通过getMainLooper取得。
public final class Looper {private static final String TAG Looper;// sThreadLocal.get() will return null unless youve called prepare().UnsupportedAppUsagestatic final ThreadLocalLooper sThreadLocal new ThreadLocalLooper();UnsupportedAppUsageprivate static Looper sMainLooper; // guarded by Looper.class// 省略public static Looper getMainLooper() {synchronized (Looper.class) {return sMainLooper;}}
}常见的用法比如在自定义的线程中。
public class MyThread extends Thread { private Handler mHandler; Override public void run() { Looper.prepare(); // 准备Looper mHandler new Handler() { Override public void handleMessage(Message msg) { // 处理消息 } } }; Looper.loop(); // 开始循环等待消息 }
}Looper的实现这里就不分析了路径在**/frameworks/base/core/java/android/os/Looper.java**可自行了解(建议先掌握epoll)
Thread
Android Thread类提供线程功能其定义在 libcore/ojluni/src/main/java/java/lang/Thread.java
public
class Thread implements Runnable {public Thread() {init(null, null, Thread- nextThreadNum(), 0);}
}调用start方法可以启动线程比如上面定义的MyThread类。
MyThread thr new MyThread();
thr.start();其提供了一些方法用于控制线程比如
sleep: 让线程等待一段时间jion等待线程退出或者叫执行完成interrupt打断线程。
注意Thread和Looper是两个事情其关系是一对一。 Thread就是常规意义上的线程程序代码最小的运行单位先不考虑协程Looper是一套基于消息(事件)的驱动机制。
Runnable是一个接口类规定了Run这个方法。MessageQueue是一个消息队列。这个类功能比较单一。其源码路径如下感兴趣的可自行了解。
/frameworks/base/core/java/android/os/MessageQueue.java/libcore/ojluni/src/main/java/java/lang/Runnable.java
再贴一遍类图加深理解。