一、 在MainActivity中为什么只是类似的写一行如下代码就可以使用handler了呢?

Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    // handle the nsg message...
  }
}; private void sendMessage() {
   handler.sendEmptyMessage(11);
}

打开handler的源码可以在它的构造函数中,看到如下的几行代码:

mLooper = Looper.myLooper();        //  (1)
if (mLooper == null) {
throw new RuntimeException("Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;

如果继续跟进 (1) 号注释中的 Looper.myLooper(); 这条语句,近儿,可以看到下面的代码:

public static Looper myLooper() {
return sThreadLocal.get(); // (2)
}

其中 sThreadLocal Looper 类中被声明为:

static final ThreadLocal<Looper> sThreadLocal = newThreadLocal<Looper>();

可得它是一个静态不可变的键值,因为它是静态成员,由类的加载过程可知在 Looper 被加载后就立即对该对象进行了初始化,而且它被声明为final类型,在接下来的生命周期中将是一个不可改变的引用,这也是称呼它为一个键值一个原因;当然,当你继续跟进时会发现,称呼它为键值原来是有更好的理由的。跟进(2)号注释中的sThreadLocal.get(); 语句,可以得到下面的逻辑:

public T get() {
// Optimized for the fast path.
Thread currentThread = Thread.currentThread(); // (3)
Values values = values(currentThread);
if (values != null) {
Object[] table = values.table;
int index = hash & values.mask;
if (this.reference == table[index]) {
return (T) table[index + 1];
}
} else {
values = initializeValues(currentThread);
} return (T) values.getAfterMiss(this); // (4)
}

我们只关注上面的代码中的 注释(3)和 (4)两行代码,其中从注释 (3)处我们得知这里的读取操作是从当前线程的内部获得数据的,而注释(4)则进一步告诉我们,它是一个以 ThreadLocal 类型的对象为键值,也就是Looper中的 static final 访问控制的 sThreadLocal 对象。奇妙的地方就在这里,因为每次调用时 ThreadLocal 虽然都是同一个 sThreadLocal 对象,但在ThreadLocal 内部它是从当前正在活动的线程中取出 Looper 对象的,所以达到了一种不同的线程调用同一个 Looper 中的同一个 sThreadLocal 对象的 get 方法,而得到的 Looper是不一样的目的。并且从注释(1)处可以得知,当我们调用 sThreadLocal.get();如果返回是一个null时,我们的调用将是失败的,也就是在从当前正在活动的线程的内部读取时还没有初始化,抛出的异常提醒我们先执行ooper.prepare()方法,跟进Looper的prepare()方法:

 private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}

可得知,它同样是通过sThreadLocal对象来存放数据的,跟进一步:

 public void set(T value) {
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values == null) {
values = initializeValues(currentThread);
}
values.put(this, value);
}

从上面的代码可得知,每个线程至多只能存放一个Looper对象,注意这里强调的是每个线程只能存放一份,并不是sThreadLocal 只能放一个,这也是线程本地化存储的秘密所在。所以我们得到一个前提条件,在调用类似new Handler(){};语句时,必须事先已执行 Looper.prepare(); 语句,而且要确保两条语句都是在同一个线程中被调用的,不然可能得不到我们期望的结果。但是为什么我们在 MainAcitivity 中没有显示调用 Looper.prepare(); 方法,而只是简单调用 new Handler(); 语句呢?原来在 MainActivity运行所在的主线程(也成UI线程,getId()得到的id号是1)被android系统启动时,就主动调用了一次Looper.prepare()方法:

public static final void More ...main(String[] args) {
SamplingProfilerIntegration.start();
Process.setArgV0("<pre-initialized>");
Looper.prepareMainLooper(); (跟进该方法)
ActivityThread thread = new ActivityThread();
thread.attach(false);
Looper.loop();
if (Process.supportsProcesses()) {
throw new RuntimeException("Main thread loop unexpectedly exited");
}
thread.detach();
String name = (thread.mInitialApplication != null)
? thread.mInitialApplication.getPackageName()
: "<unknown>";
Log.i(TAG, "Main thread of " + name + " is now exiting");
}
}

二 、 Handler中是什么原理,使得发送的消息时只要使用handler对象,而在消息被接受并处理时就可以直接调用到handler中覆写的handleMessge方法?

就从下面的这行代码开始:

private void sendMessage() {
handler.sendEmptyMessage(0x001);
}

在handler类中一直跟进该方法,可以发现下面的几行代码:

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this; //(5)
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}

在上面的注释 (5) 处可以看到,这里对每个msg中的target字段进行了设置,这里就是设置了每个消息将来被处理时用到的handler对象。

可以跟进Looper方法的loop()方法得到:

 public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity(); for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
} // This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
} msg.target.dispatchMessage(msg); // (6) if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
} // Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
} msg.recycleUnchecked();
}
}

在注释 (6)处可以看到, msg.target.dispatchMessage(msg); 原来是调用的Message的 dispatchMessage()方法,不妨我们跟进去看看:

/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg); //(7)
}
}

一系列优先考虑的回调判断之后,终于轮到了自定义的处理的函数处理。

三、遇到This Handler class should be static or leaks might occur 警告 为什么?

大致意思是说:Handler 类应该为static类型,否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的引用。如果Handler是个内部类,那 么它也会保持它所在的外部类的引用。为了避免泄露这个外部类,应该将Handler声明为static嵌套类,并且使用对外部类的弱应用。说的很明白,因为内部类对外部类有个引用,所以当我们在内部类中保存对外部类的弱引用时,这里的内部类对外引用还是存在的,这是jdk实现的,而只有我们声明为静态内部类,此时是对外部类没有强引用的(这时它和普通的外部类没有什么区别,只是在访问控制上有很大的方便性,它可以直接访问外部类的私有成员),这样当线程中还有 Looper  和 Looper 的 MessageQueue 还有 Message 时( message 中的 target 保持了对 Handler 的引用),而此时 Activity已经死亡时,就可以对Activity 进行回收了。

四、注意事项

1.  在自己定义的子线程中如果想实现handler?

Looper.prepare();
handler=new Handler();
...
Looper.loop();

这几行代码一定要在run方法中调用,而不是在自定义的 Thread 子类的构造函数中调用。因为子线程的构造方法被调用时,其实子线程还没有执行,也就是说当前的线程并不是子线程,还仍然是父线程,所以调用 Looper.prepare(); 也只是对父线程进行了线程本地化存储了一个 Looper 对象。

2.  简而言之

两个线程之间用 handler 进行通信,一个线程先在线程本地存放一个Looper对象,该对象中有消息队列 MessageQueue 成员,只是这个Looper对象,是在线程运行Looper.prepare() 时初始化的,且保证只初始化一次,进而该线程进入Loop.loop()消息循环状态,等待其它线程调用自己的 hanldler 对象的 sendMessage() 方法往自己的消息队列中存放消息,并在有消息时读取并处理,没有则阻塞。

2.  多方协作

如果同时有多个调用方使用一个同一工作线程(调用Looper.loop()的那个线程),则可以先创建一个HandlerThread线程,然后调用它的getLooper方法得到一个Looper对象,

这时每个调用方都可以拿着这个Looper对象去构造满足各自处理逻辑的的Handler对象了,然后需要出的时候将消息丢到自己的Handler对象中即可。

android 中的 Handler 线程间通信的更多相关文章

  1. Java 中如何实现线程间通信

    世界以痛吻我,要我报之以歌 -- 泰戈尔<飞鸟集> 虽然通常每个子线程只需要完成自己的任务,但是有时我们希望多个线程一起工作来完成一个任务,这就涉及到线程间通信. 关于线程间通信本文涉及到 ...

  2. Handler线程间通信

    package com.hixin.appexplorer; import java.util.List; import android.app.Activity; import android.ap ...

  3. Android中线程间通信原理分析:Looper,MessageQueue,Handler

    自问自答的两个问题 在我们去讨论Handler,Looper,MessageQueue的关系之前,我们需要先问两个问题: 1.这一套东西搞出来是为了解决什么问题呢? 2.如果让我们来解决这个问题该怎么 ...

  4. 源码分析Android Handler是如何实现线程间通信的

    源码分析Android Handler是如何实现线程间通信的 Handler作为Android消息通信的基础,它的使用是每一个开发者都必须掌握的.开发者从一开始就被告知必须在主线程中进行UI操作.但H ...

  5. Android线程间通信机制——深入理解 Looper、Handler、Message

    在Android中,经常使用Handler来实现线程间通信,必然要理解Looper , Handler , Message和MessageQueue的使用和原理,下面说一下Looper , Handl ...

  6. 线程间通信(等待,唤醒)&Java中sleep()和wait()比较

    1.什么是线程间通信? 多个线程在处理同一资源,但是任务却不同. 生活中栗子:有一堆煤,有2辆车往里面送煤,有2辆车往外拉煤,这个煤就是同一资源,送煤和拉煤就是任务不同. 2.等待/唤醒机制. 涉及的 ...

  7. Object类中wait带参方法和notifyAll方法和线程间通信

    notifyAll方法: 进入到Timed_Waiting(计时等待)状态有两种方式: 1.sleep(long m)方法,在毫秒值结束之后,线程睡醒,进入到Runnable或BLocked状态 2. ...

  8. Object类中wait代餐方法和notifyAll方法和线程间通信

    Object类中wait代餐方法和notifyAll方法 package com.yang.Test.ThreadStudy; import lombok.SneakyThrows; /** * 进入 ...

  9. Android中的Handler及它所引出的Looper、MessageQueue、Message

    0.引入 0.1.线程间通信的目的 首先,线程间通信要交流些什么呢? 解答这个问题要从为什么要有多线程开始,需要多线程的原因大概有这些 最早也最基本:有的任务需要大量的时间,但其实并不占用计算资源,比 ...

随机推荐

  1. js 数字键盘

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和

    B. Alice, Bob, Two Teams 题目连接: http://www.codeforces.com/contest/632/problem/B Description Alice and ...

  3. Codeforces Round #339 (Div. 1) B. Skills 暴力 二分

    B. Skills 题目连接: http://www.codeforces.com/contest/613/problem/B Description Lesha plays the recently ...

  4. Win32 error code message

    http://fit.c2.com/fit/files/LispPlatform/lisp/clisp-2.28/src/errwin32.d # Calls a function, passing ...

  5. (转)c#中判断是不是数字和字母

    一.判断字母string str = Console.ReadLine();if (char.isLetter(str)){ }else if (char.IsDigit(str)){ }if(ch& ...

  6. ARM体系下的GCC内联汇编

    转:http://andyhuzhill.github.io/arm/gcc/asm/2012/09/25/gcc-inline-assemly/ 在操作系统级的编程中,有时候,C语言并不能完全的使用 ...

  7. Unity3D 避免玩家作弊

    如果你的Unity项目快上线了,我强烈建议你看一下Anti-Cheat这个插件.因为IOS和Android分别越狱和Root后玩家可以使用 @八门神器 @烧饼修改器 等一些列作弊的软件来修改游戏内存, ...

  8. SQL 的四种分类 DDL,DML,DCL,TCL

    DDL (数据定义问题) 数据定义语言 - Data Definition Language 用来定义数据库的对象,如数据表.视图.索引等DDL不需要commit.CREATEALTERDROPTRU ...

  9. js中定义变量的三种方式const,val,let 的区别

    js中三种定义变量的方式const, var, let的区别. 1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始 ...

  10. Linux编程中的坑——C++中exit和return的区别

    今天遇到一个坑,折腾了一天才把这个坑填上,情况是这样的: 写了段代码,在main()函数中创建一个分离线程,结果这个线程什么都没干就直接挂掉了,代码长这样: int main() { 创建一个分离线程 ...