zoukankan      html  css  js  c++  java
  • Android:用Handler实现异步处理功能

    Android:用Handler实现异步处理功能 - 51CTO.COM

    一.一个问题

    有这样一个问题值得我们思考,若把一些类似于下载的功能(既耗时且不一定有结果)写在Activity(主线程)里,会导致Activity阻塞,长时间无响应,直至页面假死(如果5秒钟还没有完成的话,会收到Android系统的一个错误提示 "强制关闭")。因此,我们需要把这些耗时的操作放在单独的子线程中操作。这就是Handler的使命。Handler提供异步处理的功能,发送和接收不是同时的(Activity的主线程和线程队列里的线程是不同的线程,并行进行,互不影响)。

    二.Handler简介

    Handler 为Android操作系统中的线程通信工具,它主要由两个作用:(1)安排消息或Runnable 在某个主线程中某个地方执行(2)安排一个动作在另外的线程中执行。每个Handler对象维护两个队列(FIFO),消息队列和Runnable队列, 都是有Android操作系统提供的。Handler可以通过这两个队列来分别:

    1. 发送、接受、处理消息–消息队列;
    2. 启动、结束、休眠线程–Runnable队列;

    Handler的使用方法大体分为3个步骤:1.创建Handler对象。2.创建Runnable和消息。3.调用post以及sendMessage方法将Runnable和消息添加到队列。

    三.Runnable队列

    1.java中的线程

    在java中,线程的创建有两种方法:继承Thread类和实现Runnable接口。而这最重要的都是要复写run方法来实现线程的功能。当线程的时间片到了,开始运行时,就执行run()函数,执行完毕,就进入死亡状态。

    举个创建线程的例子:

    1. Runnable r=new Runnable(){ 
    2.  
    3. @Override 
    4. public void run() { 
    5. // TODO Auto-generated method stub 
    6. System.out.println("thread"); 
    7. handler.postDelayed(thread, 3000); 
    8. }; 

    2.关于Runnable队列

    (1)原理

    Android的线程异步处理机制:Handler对象维护一个线程队列,有新的Runnable送来(post())的时候,把它放在队尾,而处理 Runnable的时候,从队头取出Runnable执行。当向队列发送一个Runnable后,立即就返回,并不理会Runnable是否被执行,执行 是否成功等。而具体的执行则是当排队排到该Runnable后系统拿来执行的。这就好比邮局的例子。寄信者将信写好后放入邮筒就回家了,他并不知道邮件何 时被邮局分发,何时寄到,对方怎样读取这些事。这样,就实现了Android的异步处理机制。

    (2)具体操作

    向队列添加线程:

    handler.post(Runnable );将Runnable直接添加入队列

    handler.postDelayed(Runnable, long)延迟一定时间后,将Runnable添加入队列

    handler.postAtTime(Runnable,long)定时将Runnable添加入队列

    终止线程:

    handler.removeCallbacks(thread);将Runnable从Runnable队列中取出

    四.消息队列

    1.消息对象

    (1)Message对象

    Message对象携带数据,通常它用arg1,arg2来传递消息,当然它还可以有obj参数,可以携带Bundle数据。它的特点是系统性能消耗非常少。

    初始化: Message msg=handler.obtainMessage();

    (2)Bundle对象

    Bundle是Android提供的类,可以把它看做是特殊的Map,即键值对的包。而它特殊在键和值都必须要是基本数据类型或是基本数据类型的数组(Map的键值要求都是对象),特别的,键要求都是String类型。用Message来携带Bundle数据:

    放入:msg.setData(Bundle bundle);

    取出:msg.getData();

    2.关于消息队列

    (1)原理

    Android的消息异步处理机制:Handler对象维护一个消息队列,有新的消息送来(sendMessage())的时候,把它放在队尾,之后排队 到处理该消息的时候,由主线程的Handler对象处理(handleMessage())。整个过程也是异步的,和Runnable队列的原理相同。

    (2)具体操作:

    向队列添加Runnable:handler.sendMessage(Message);

    将消息发送到消息队列msg.sendToTarget();

    延迟一定时间后,将消息发送到消息队列 handler.sendMessageDelayed(Message,long);

    定时将消息发送到消息队列 handler.sendMessageAtTime(Message,long)

    处理消息:

    消息的具体处理过程,需要在new Handler对象时使用匿名内部类重写Handler的handleMessage(Message msg)方法,如下:

    1. Handler handler=new Handler(){ 
    2.  
    3. @Override 
    4. public void handleMessage(Message msg) { 
    5. // TODO Auto-generated method stub 
    6. 。。。。。。 
    7.  
    8. 。。。。。。 
    9. }; 

    五.Handler的两个作用

    1.安排消息或Runnable 在某个主线程中某个地方执行

    代码示例:

    1. public class HandlerTestActivity extends Activity { 
    2. private Button start; 
    3. @Override 
    4. protected void onCreate(Bundle savedInstanceState) { 
    5. // TODO Auto-generated method stub 
    6. super.onCreate(savedInstanceState); 
    7. setContentView(R.layout.handlertest); 
    8. start=(Button) findViewById(R.id.start); 
    9. start.setOnClickListener(new startListener()); 
    10.  
    11. System.out.println("Activity Thread:"+Thread.currentThread().getId()); 
    12. Handler handler=new Handler(); 
    13. Runnable thread=new Runnable(){ 
    14.  
    15. @Override 
    16. public void run() { 
    17. // TODO Auto-generated method stub 
    18. System.out.println("HandlerThread:"+Thread.currentThread().getId()); 
    19.  
    20. }; 
    21. class startListener implements OnClickListener{ 
    22.  
    23. @Override 
    24. public void onClick(View v) { 
    25. // TODO Auto-generated method stub 
    26. handler.post(thread); 
    27.  

    这个小程序中,首先程序启动,进入onCreate(),打印出当前线程(即主线程)的ID,之后点击按钮start,会将线程thread添加到线程队 列,执行线程thread,thread的作用就是打印出当前线程的ID。在这个程序中,我们可以看到通过Handler我们可以实现安排 Runnable 在某个主线程中某个地方执行,即作用(1)。

    不过这里有个小小的陷阱,你发现了吗?这个程序看上去似乎实现了Handler的异步机制, handler.post(thread)似乎实现了新启线程的作用,不过通过执行我们发现,两个线程的ID相同!也就是说,实际上thread还是原来 的主线程,由此可见,handler.post()方法并未真正新建线程,只是在原线程上执行而已,我们并未实现异步机制。

    2.安排一个动作在另外的线程中执行。

    (1)java中标准的创建线程的方法

    第一步:

    1.  Runnable r=new Runnable(){ 
    2.  
    3. @Override 
    4. public void run() { 
    5. // TODO Auto-generated method stub 
    6. System.out.println("thread"); 
    7. handler.postDelayed(thread, 3000); 
    8. }; 

    第二步:

    1. Thread t=new Thread (r); 

    第三步:

    1. t.start(); 

    若把上面示例程序中的handler.post(thread);语句改成以上形式,通过打印我们可以看到,两个ID是不同的,新的线程启动了!

    (2)关于Looper

    Looper类用来为线程开启一个消息循环,作用是可以循环的从消息队列读取消息,所以Looper实际上就是消息队列+消息循环的封装。每个线程只能对应一个Looper,除主线程外,Android中的线程默认是没有开启Looper的。

    通过Handler与Looper交互,Handler可以看做是Looper的接口,用来向指定的Looper发送消息以及定义处理方法。默认情况下Handler会与其所在线程的Looper绑定,即:

    Handler handler=new Handler();等价于Handler handler=new Handler(Looper.myLooper());

    Looper有两个主要方法:

    Looper.prepare();启用Looper
    Looper.loop(); 让Looper开始工作,从消息队列里取消息,处理消息。

    注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。

    (3)Handler异步机制的实现

    Handler是通过HandlerThread 使得子线程与主线程分属不同线程的。实际上,HandlerThread 是一个特殊的线程,它是一个封装好Looper的线程,

    代码示例:

    1.  //创建一个名叫handler_hread的HandlerThread 对象 
    2. HandlerThread handlerThread=new HandlerThread("handler_hread"); 
    3.  
    4. //开启handlerThread,在使用handlerThread.getLooper()之前必须先调用start方法,否则取出的是空 
    5. handlerThread.start(); 
    6.  
    7. //将handler绑定在handlerThread的Looper上,即这个handler是运行在handlerThread线程中的 
    8. myHandler handler=new myHandler(handlerThread.getLooper()); 
    9.  
    10. class myHandler extends Handler{ 
    11. public myHandler(){} 
    12. public myHandler(Looper looper){ 
    13. super(looper); 
    14. @Override 
    15. public void handleMessage(Message msg) { 
    16. // TODO Auto-generated method stub 
    17. System.out.println("Activity Thread:"+Thread.currentThread().getId()); 
    18. }

    这样,就实现了handler的异步处理机制,在调用handler.post()方法,通过打印线程ID可以得知,子线程与主线程是分属不同线程的。

    ----------------------------------------------------------------------

    google对handler的说明

    A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

    There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

    Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

    When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

    When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

  • 相关阅读:
    Scala编程基础
    大数据学习环境搭建(CentOS6.9+Hadoop2.7.3+Hive1.2.1+Hbase1.3.1+Spark2.1.1)
    hadoop2.7.3编译,支持snappy、bzip2本地压缩
    ABAP非Unicode系统中字符串拼接(CONCATENATE)时吃字符问题
    Hadoop学习笔记
    HIVE开发总结
    中文字符截取乱码问题
    替换空字符NULL(字符编码为0的字符)
    Generate Time Data(普通日期主数据)
    Generate Time Data(财务日期主数据)
  • 原文地址:https://www.cnblogs.com/seven1979/p/4166460.html