从源码看Handler和Runnable Thread 以及HandlerThread的关系

版权声明:转载请注明出处。作者:两仪织,博客地址:http://blog.csdn.net/u013894427 https://blog.csdn.net/u013894427/article/details/84637542

Handler的构造函数

Handler是Android中的概念,其构造函数有以下几种

public Handler();
public Handler(Callback callback);
public Handler(Looper looper);
public Handler(Looper looper, Callback callback);
public Handler(boolean async);
public Handler(Callback callback, boolean async);
public Handler(Looper looper, Callback callback, boolean async);

Callback这个接口的定义是这样子的,是不是已经很熟悉了。

public interface Callback 
{
	public boolean handleMessage(Message msg);
}

理解Handler和HandlerThread的关系需要对Looper有一定的理解。
Looper是Android的概念,我们只需要知道,在Android里面,每一个线程都有一个,如果Handler没有指定,那么就表示是Handler初始化所在线程。

Handler和Runnable的关系

Runnable是Java的概念,是一个接口,定位是,如果一个类的实例想在一个线程中执行,就应该实现Runnable接口,这个类必须定义一个无参数函数run()

Runnable的接口定义如下,

/**
 * The <code>Runnable</code> interface should be implemented by any
 * class whose instances are intended to be executed by a thread. The
 * class must define a method of no arguments called <code>run</code>.
 * <p>
 * This interface is designed to provide a common protocol for objects that
 * wish to execute code while they are active. For example,
 * <code>Runnable</code> is implemented by class <code>Thread</code>.
 * Being active simply means that a thread has been started and has not
 * yet been stopped.
 * <p>
 * In addition, <code>Runnable</code> provides the means for a class to be
 * active while not subclassing <code>Thread</code>. A class that implements
 * <code>Runnable</code> can run without subclassing <code>Thread</code>
 * by instantiating a <code>Thread</code> instance and passing itself in
 * as the target.  In most cases, the <code>Runnable</code> interface should
 * be used if you are only planning to override the <code>run()</code>
 * method and no other <code>Thread</code> methods.
 * This is important because classes should not be subclassed
 * unless the programmer intends on modifying or enhancing the fundamental
 * behavior of the class.
 *
 * @author  Arthur van Hoff
 * @see     java.lang.Thread
 * @see     java.util.concurrent.Callable
 * @since   JDK1.0
 */
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

在Handler.java文件中搜索Runnable,还是有结果的,就是这个Post系列方法,作用是在Handler对象绑定的线程中执行Runnable中的run函数。

public final boolean post(Runnable r);
public final boolean postAtTime(Runnable r, long uptimeMillis);
public final boolean postAtTime(Runnable r, Object token, long uptimeMillis);
public final boolean postDelayed(Runnable r, long delayMillis);
public final boolean postDelayed(Runnable r, Object token, long delayMillis);
public final boolean postAtFrontOfQueue(Runnable r);

post(Runnable r)函数定义

/**
 * Causes the Runnable r to be added to the message queue.
 * The runnable will be run on the thread to which this handler is 
 * attached. 
 *  
 * @param r The Runnable that will be executed.
 * 
 * @return Returns true if the Runnable was successfully placed in to the 
 *         message queue.  Returns false on failure, usually because the
 *         looper processing the message queue is exiting.
 */
public final boolean post(Runnable r)
{
   return  sendMessageDelayed(getPostMessage(r), 0);
}

Handler和Thread的关系

Thread也是Java的概念,它是Java提供的功能比较多的实现了Runnable接口的类,但是同样的,Thread没有Looper这个概念。所以除非Thread作为Runnable传入上面所说的post系列函数中,否则和

Handler和HandlerThread的关系

Android为了和Handler配套使用Thread,或者说为了配套使用Looper,继承了Thread实现了一个新类,这个类就是HandlerThread.

继承关系和结构如图所示
在这里插入图片描述
在这里插入图片描述

所以使用的时候Handler在使用HandlerThread的时候实际上就是和getLooper。

举个例子

HandlerThread handlerThread=new HandlerThread("validate demo");
handlerThread.start();
Handler handler=new Handler(handlerThread.getLooper());

好了,以上就是从源码看Handler和Runnable Thread 以及HandlerThread的关系全文,如有错误,请在评论区斧正,感谢!

猜你喜欢

转载自blog.csdn.net/u013894427/article/details/84637542