优化项目过程中发现了一个非常Low的问题,整理一下。备忘:
说问题之前先看下HandlerThread的定义
一个封装了looper的线程:  

Looper用于封装了android线程中的消息循环。默认情况下一个线程是不存在消息循环(message loop)的,须要调用Looper.prepare()来给线程创建一个消息循环,调用Looper.loop()来使消息循环起作用。从消息队列里取消息。处理消息。
注:写在Looper.loop()之后的代码不会被马上执行。当调用后mHandler.getLooper().quit()后。loop才会中止,其后的代码才干得以执行。

Looper对象通过MessageQueue来存放消息和事件。一个线程仅仅能有一个Looper。相应一个MessageQueue。

下面是Android API中的一个典型的Looper thread实现:

//Handler不带參数的默认构造函数:new Handler()。实际上是通过Looper.myLooper()来获取当前线程中的消息循环,

//而默认情况下。线程是没有消息循环的,所以要调用 Looper.prepare()来给线程创建消息循环,然后再通过。Looper.loop()来使消息循环起作用。

[java] view
plain
copy

  1. class LooperThread extends Thread
  2. {
  3. public Handler mHandler;
  4. public void run()
  5. {
  6. Looper.prepare();
  7. mHandler = new Handler()
  8. {
  9. public void handleMessage(Message msg)
  10. {
  11. // process incoming messages here
  12. }
  13. };
  14. Looper.loop();
  15. }

另,Activity的MainUI线程默认是有消息队列的。

所以在Activity中新建Handler时,不须要先调用Looper.prepare()。

那么遇到了有多Low的问题呢:

项目中重写了一个HandlerThread,然后定义了post方法。然后在主线程中例如以下实现:

AsyncHandler.post(new Runnable() {

            @Override

            public void run() {

                try {

                    Looper.prepare();

// 一坨要异步运行的代码******

Looper.loop();

                } catch (Exception e) {

                    // TODO: handle exception

                e.printStackTrace();

                }

那么明眼人一看就看出问题来了 ,这代码一跑异步代码肯定运行不到啊。为啥呢。且看下prepare的实现:





     /** Initialize the current thread as a looper.

      * This gives you a chance to create handlers that then reference

      * this looper, before actually starting the loop. Be sure to call

      * {@link #loop()} after calling this method, and end it by calling

      * {@link #quit()}.

      */

    public static void prepare() {

        prepare(true);

    }





    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));

    }

So,简单,却是问题~

Android 线程 Looper.prepare()、Looper.loop() 使用的更多相关文章

  1. 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  2. 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  3. 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  4. Android消息队列和Looper

    1. 什么是消息队列 消息队列在android中对应MessageQueue这个类,顾名思义,消息队列中存放了大量的消息(Message) 2.什么是消息 消息(Message)代表一个行为(what ...

  5. Android消息机制:Looper,MessageQueue,Message与handler

    Android消息机制好多人都讲过,但是自己去翻源码的时候才能明白. 今天试着讲一下,因为目标是讲清楚整体逻辑,所以不追究细节. Message是消息机制的核心,所以从Message讲起. 1.Mes ...

  6. Android -- Looper.prepare()和Looper.loop() —深入版

    Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理.handler其实可以看做是一个工具类,用来向消息队列中插入消息的. (1) Loope ...

  7. 【转】Android -- Looper.prepare()和Looper.loop()

    Looper.prepare()和Looper.loop() 原文地址:http://blog.csdn.net/heng615975867/article/details/9194219 Andro ...

  8. Android -- Looper.prepare()和Looper.loop() —深度版

    Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理.handler事实上能够看做是一个工具类.用来向消息队列中插入消息的. (1) Loop ...

  9. Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误

    原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...

随机推荐

  1. 亚马逊EC2构建代理服务器心血历程

    1.亚马逊上申请一台免费的EC2服务器,有相应的教程,绑定信用卡,预支付1美元,据说可以退回(防止到期后直接扣款,支付后通过修改卡信息,但好象有提示了,说卡不对了,也不管它了,到期后再说,美国人也不是 ...

  2. ProE复杂曲线方程:Python Matplotlib 版本代码(L系统,吸引子和分形)

    对生长自动机的研究由来已久,并在计算机科学等众多学科中,使用元胞自动机的概念,用于生长模拟.而复杂花纹的生成,则可以通过重写一定的生长规则,使用生成式来模拟自然纹理.当然,很多纹理是由人本身设计的,其 ...

  3. codeforces_731C_[dfs][并查集]

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. CAD设置当前显示的光标(com接口VB语言)

    主要用到函数说明: MxDrawXCustomFunction::Mx_SetCursor 设置当前显示的光标,光标可以从cur文件加载,详细说明如下: 参数 说明 CString sCursorFi ...

  5. python包与模块

    Python基础-包与模块 摘要 为重用以及更好的维护代码,Python使用了模块与包:一个Python文件就是一个模块,包是组织模块的特殊目录(包含__init__.py文件). 模块搜索路径,Py ...

  6. mitmproxy安装与使用

    mitmproxy安装与使用 (抓包,中间人代理工具.支持SSL) 在开发微信公端的时候开发调试只能用浏览器自带开发工具,本来移动端可以用用fiddler.wireshark等工具来抓包,但是自从改用 ...

  7. Django - 创建多对多及增加示例

    创建多对多: 方式一: 自定义关系表 备注:自定义表Host.Application,通过自定义表,将表Host和Application进行关联(通过外键方式工): 执行语句:python manag ...

  8. 环形缓冲区: ringbuf.c

    #cat aa.c /*ringbuf .c*/ #include<stdio.h> #include<ctype.h> #define NMAX 8 int iput = 0 ...

  9. windows下python-nmap运行过程中出现的问题及解决办法

    python-nmap 运行时出现了一下错误 D:\python\untitled5\Scripts\python.exe D:/python/untitled5/test.py Traceback ...

  10. file.seek()/tell()-笔记

    ---------------------------------------------------------------------------------------------------- ...