美文网首页
模拟实现android的Handler工作机制

模拟实现android的Handler工作机制

作者: 最美下雨天 | 来源:发表于2018-06-07 15:22 被阅读10次

转载自:https://blog.csdn.net/qingchunweiliang/article/details/50448365

public class Handler {  
  
  
    private MessageQueue messageQueue;  
      
    public Handler() {  
  
        Looper looper=Looper.myLooper();  
          
        if (looper==null) {  
             throw new RuntimeException(  
                        "Can't create handler inside thread that has not called Looper.prepare()");  
                 
        }  
          
        this.messageQueue=looper.messageQueue;  
    }  
  
    public void sendMessage(Message msg) {  
          
        //Looper循环中发现message后,调用message.targer就得到了当前handler,使用taget.handleMessage  
        //就把消息转发给了发送message时的handler的handleMessage函数  
        msg.target=this;  
          
        messageQueue.enqueueMessage(msg);  
          
    }  
      
    public void handleMessage(Message msg) {  
    }  
}
public class Looper {
    private static final ThreadLocal<Looper> threadLocal=new ThreadLocal<>();
    /**
     * 存储Message的队列,阻塞式,没有消息则一直等待
     */
    final MessageQueue messageQueue;
    
    
    private Looper() {
        messageQueue=new MessageQueue();
    }
    /**为该线程创建Looper,
     * 若该线程已经有Looper了则不需要再次调用prepare
     */
    public  static void prepare() {
        if (threadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        threadLocal.set(new Looper() );
    }
    
    public static void loop() {
        Looper looper=myLooper();
        if (looper == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        MessageQueue messageQueue=looper.messageQueue;
        
        for(;;){
            Message message=messageQueue.next();
            message.target.handleMessage(message);
        }
    }
    
    /**
     * 获取当先线程的Looper
     * @return
     */
    public static Looper myLooper() {
        return threadLocal.get();
    }
}
public class Message {  
  
    Handler target;  
    public Object obj;  
    public int what;  
  
    @Override  
    public String toString() {  
        return   "what="+what+" obj="+obj.toString();  
    }  
      
}

import java.util.concurrent.BlockingQueue;  
import java.util.concurrent.LinkedBlockingQueue;  
  
public class MessageQueue {  
  
  
    private BlockingQueue<Message>blockingQueue=new LinkedBlockingQueue<>();  
      
    /** 
     * 阻塞式,没有消息则一直等待 
     * @return 
     */  
    public Message next() {  
        try {  
            return blockingQueue.take();  
        } catch (InterruptedException e) {  
            throw new RuntimeException();  
        }  
    }  
      
    /** 
     * 插入到消息队列尾部 
     * @param message 
     */  
    void enqueueMessage(Message message) {  
        try {  
            blockingQueue.put(message);  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
}
import java.util.HashMap;  
import java.util.Map;  
/** 
 * ThreadLocal简单实现 
 * @author Young 
 * 
 * @param <T> 
 */  
public class ThreadLocal<T> {  
  
  
    private Map<Thread,T>map;  
  
    public ThreadLocal() {  
        map=new HashMap<>();  
    }  
      
    public void set(T obj) {  
        map.put(Thread.currentThread(),obj);  
    }  
      
    public T get() {  
        return map.get(Thread.currentThread());  
    }  
      
}
public class Main {
    public static void main(String[] args) {
        new Main().start();

    }

    private void start() {
        //创建该线程唯一的消息队列,线程安全的阻塞队列
        Looper.prepare();
        onCreate();

        //死循环,阻塞式,执行下面代码后主线程就会去获取消息队列里的消息,没有消息时就阻塞,有就执行。执行Looper.loop前即使消息队列里有消息,消息也不会执行,因为主线程还没有去检查消息队列。
        Looper.loop();

        //下面 的代码通常不会执行,除非手动让主线程消息队列退出。退出主线程消息队列后android的view布局、绘制,事件分发就不执行了,所以android APP也没必要继续执行了,所以android采用了抛出异常的方式结束APP。
        System.out.println("exit........");
        throw new RuntimeException("Main thread loop unexpectedly exited");


    }

    private void onCreate() {
        //下面的操作相当于运行在android的UI线程中
        final Thread thread = Thread.currentThread();
        System.out.println("main thread=" + thread);
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                //若thread == Thread.currentThread(),则证明已经运行在主线程中了
                System.out.println("current thread is main thread? " + (thread == Thread.currentThread()));
                System.out.println(msg);
                System.out.println();
            }
        };
        // 测试1       主线程创建handler,子线程使用该handler发送消息
        new Thread() {
            public void run() {
                try {//模拟耗时操作
                    Thread.sleep(1000 * 2);
                } catch (InterruptedException e) {
                }
                Message message = new Message();
                message.obj = "new Thread" + Thread.currentThread();
                message.what = (int) System.currentTimeMillis();
                //在子线程中发送消息
                handler.sendMessage(message);

                try {
                    Thread.sleep(1000 * 2);
                } catch (InterruptedException e) {
                }

                message = new Message();
                message.obj = "hanler...waht==1";
                message.what = 1;
                //在子线程中发送消息
                handler.sendMessage(message);

                try {
                    Thread.sleep(1000 * 2);
                } catch (InterruptedException e) {
                }
                message = new Message();
                message.obj = "hanler...waht==2";
                message.what = 2;
                //在子线程中发送消息
                handler.sendMessage(message);

                try {
                    Thread.sleep(1000 * 2);
                } catch (InterruptedException e) {
                }
                message = new Message();
                message.obj = "hanler...waht==3";
                message.what = 3;
                //在子线程中发送消息
                handler.sendMessage(message);

            }

            ;
        }.start();
        // 测试2 在thread内部创建handler,结果会抛出异常
        new Thread() {
            public void run() {
                try {
                    sleep(1000 * 3);
                } catch (InterruptedException e) {
                }
                /*
                 * 在线程内部使用默认构造函数创建handler会抛出异常。
                 * android中也可以在子线程中创建Handler,但要在初始化时传入Looper,
                 * Looper.getMainLooper()获取到的就是主线程的Looper,所以可以这样创建
                 * 
                 * new Handler(Looper.getMainLooper()){
                        @Override
                        public void handleMessage(Message msg) {
                            //运行在主线程中
                        }
                    };
                 */
                Handler h = new Handler() {
                    public void handleMessage(Message msg) {
                        System.out.println("haneler msg...." + msg);
                    }
                };
                Message message = new Message();
                message.obj = "handler in new Thread";
                message.what = (int) System.currentTimeMillis();
                //在子线程中发送消息
                h.sendMessage(message);
            }
        }.start();
        //上面的操作相当于运行在android的UI线程中
    }
}

运行结果:


image.png

程序并没有停止,主线程消息队列仍旧阻塞等待新消息到来

相关文章

网友评论

      本文标题:模拟实现android的Handler工作机制

      本文链接:https://www.haomeiwen.com/subject/pruawxtx.html