当前位置:操作系统 > 安卓/Android >>

Android实战技巧:Handler与线程间通信ITC

在上篇http://www.zzzyk.com/kf/201205/131588.html文章中谈到了Handler是用于操作线程内部的消息队列,所以Handler可以用来线程间通信ITC,这种方式更加安全和高效,可以大大减少同步的烦恼,甚至都可以不用syncrhonized。

线程间通讯ITC
正常情况下函数调用栈都会生存在同一个线程内,想要把执行逻辑交换到其他线程可以新建一个Thread,然后start()。另外一种方法就是用ITC,也即用消息队列来实现,线程需要把执行逻辑交到其他线程时就向另外的线程的消息队列发送一个消息,发送消息后函数就此结束返回,调用栈也停止。当消息队列中有了消息时,线程会被唤醒来执行处理消息,从而把执行逻辑从一个线程转到另外一个线程。这就实现了线程间的通信ITC,与进行间通讯IPC有十分类似的思想。
通常的做法都是,在主线程创建一个Handler,然后在新建线程中使用此Handler与主线程通讯。因为主线程的消息队列已经建好,所以直接创建Handler即可,新建的线程就可以直接使用。
有些情况,需要在多线程之间进行通信,这就要为每个线程都创建MessageQueue和Handler,只要线程能访问其他线程的Handler就可以与之通信。

要正确的创建Handler,因为Handler要与线程绑定,所以在初始化Handler的时候就要注意:

如果给Handler指定Looper对象new Handler(Looper),那么此Handler便绑定到Looper对象所在的线程中,Handler的消息处理回调会在那个线程中执行。

如果创建线程时不指定Looper对象,那么此Handler绑定到创建此Handler的线程内,消息回调处理会在那个线程中执行,所以像下面的例子,如果这样写:

[java]
private class CookServer extends Thread { 
       private Handler mHandler = new Handler() { 
               public void handleMessage(Message msg) { 
                     .... 
                } 
        }; 
那么,此mHandler会与创建此CookerServer的线程绑定,handleMessage也会运行于其中。显然,如果是主线程调用new CookServer(),那么mHandler其实是运行在主线程中的。正确的写法应该是:
[java]
private class CookServer extends Thread { 
       public void run() { 
             Looper.prepare(); 
                 // or new Handler(Looper.myLooper()) 
                 private Handler mHandler = new Handler() { 
                       public void handleMessage(Message msg) { 
                     .... 
                } 
        }; 
HandlerThread
如果要在一个线程中使用消息队列和Handler,Android API中已经有封装好了的一个类HandlerThread,这个类已经做好了Looper的初始化工作,你需要做的就是重写其onLooperPrepared()方法,在其中创建Handler:

[java] private class DeliverServer extends HandlerThread { 
      private Handler mHandler; 
      public DeliverServer(String name) { 
           super(name); 
      } 
 
      @Override 
      public void onLooperPrepared() { 
            mHandler = new Handler(getLooper()) { 
                    public void handleMessage(Message msg) { 
                        ..... 
                    } 
             }; 
       } 


实例
此实例模拟了一个网络订餐系统,客户点击“Submit order"来产生一个定单,主线程中负责收集定单,然后交由CookServer来制作,CookServer在制作完成后会交由DeliverServer来把食物运送到客户,至此一个定单完成,同时CookServer和DeliverServer会更新状态。

\

[java]
/**
 * How to attach an Handler to a Thread:
 * If you specify Looper object to Handler, i.e. new Handler(Looper), then the handler is attached to the thread owning
 * the Looper object, in which handleMessage() is executed.
 * If you do not specify the Looper object, then the handler is attached to the thread calling new Handler(), in which
 * handleMessage() is executed.
 * In this example, for class CookServer or DeliverServer, if you write this way:
 *     private class CookServer extends Thread {
        private Handler mHandler;
        private Looper mLooper;
    
        public CookServer() {
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    ....
                }
                start();
        }
 * then mHandler is attached to thread calling new CookServer(), which is the main thread, so mHandler.handleMessage() will
 * be executed in main thread.
 * To attach mHandler to its own thread, you must put it in run(), or after mLooper is created. For our example, providing
 * mLooper or not won't matter, because new Handler() is called in run(), which is in a new thread.
 */ 
public class HandlerITCDemo extends ListActivity { 
    private static final int COOKING_STARTED = 1; 
    private static final int COOKING_DONE = 2; 
    private static final int DELIVERING_STARTED = 3; 
    private static final int ORDER_DONE = 4; 
     
    private ListView mListView; 
    private static final String[] mFoods = new String[] { 
    "Cubake", 
    "Donut", 
    "Eclaire", 
    "Gingerbread", 
    "Honeycomb", 
    "Ice Cream Sanwitch", 
    "Jelly Bean", 
    }; 
    private ArrayList<String&

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,