Handler、Thread和Runnable简单分析

  Handler、Thread和Runnable在开发中频繁使用,很多新手都因为概念不清而头绪全无,在这我来简单得缕缕这三者的联系与区别。

  Runnable是最简单的,它并没有什么包装,Android源码如下:

 1 /**
 2  * Represents a command that can be executed. Often used to run code in a
 3  * different {@link Thread}.
 4  */
 5 public interface Runnable {
 6
 7     /**
 8      * Starts executing the active part of the class‘ code. This method is
 9      * called when a thread is started that has been created with a class which
10      * implements {@code Runnable}.
11      */
12     public void run();
13 }

  Runnable就是一个非常简单的接口,注释上说的是“代表一个能被执行的命令,总是用来在新的线程中运行”。
  我们再来看看Runnable的子类Thread,我们经常使用Thread来新建一个线程脱离原线程来单独跑,也经常把Runnable的实现类用Thread来包装成线程的主要执行内容:Thread thread = new Thread(Runnable)。我们就先来屡屡Thread thread = new Thread(Runnable)的过程。

 1     /**
 2      * Constructs a new {@code Thread} with a {@code Runnable} object and a
 3      * newly generated name. The new {@code Thread} will belong to the same
 4      * {@code ThreadGroup} as the {@code Thread} calling this constructor.
 5      *
 6      * @param runnable
 7      *            a {@code Runnable} whose method <code>run</code> will be
 8      *            executed by the new {@code Thread}
 9      *
10      * @see java.lang.ThreadGroup
11      * @see java.lang.Runnable
12      */
13     public Thread(Runnable runnable) {
14         create(null, runnable, null, 0);
15     }

  注释说用Runnable来构造一个线程实例,且创建的线程属于相同的ThreadGroup(是一种线程容器,create方法的第一个参数代表这个),我们来看看create方法都做了什么。

/**
     * Initializes a new, existing Thread object with a runnable object,
     * the given name and belonging to the ThreadGroup passed as parameter.
     * This is the method that the several public constructors delegate their
     * work to.
     *
     * @param group ThreadGroup to which the new Thread will belong
     * @param runnable a java.lang.Runnable whose method <code>run</code> will
     *        be executed by the new Thread
     * @param threadName Name for the Thread being created
     * @param stackSize Platform dependent stack size
     * @throws IllegalThreadStateException if <code>group.destroy()</code> has
     *         already been done
     * @see java.lang.ThreadGroup
     * @see java.lang.Runnable
     */
    private void create(ThreadGroup group, Runnable runnable, String threadName, long stackSize) {
        Thread currentThread = Thread.currentThread();
        if (group == null) {
            //前面我们说过了,用Runnable新建的线程和原线程属于同一线程容器
            group = currentThread.getThreadGroup();
        }
        if (group.isDestroyed()) {
            throw new IllegalThreadStateException("Group already destroyed");
        }

        this.group = group;
        //此处用synchronized来保证新建的线程id+1并且使唯一的
        synchronized (Thread.class) {
            id = ++Thread.count;
        }
        //threadName
        if (threadName == null) {
            this.name = "Thread-" + id;
        } else {
            this.name = threadName;
        }
        //相当于目标任务
        this.target = runnable;
        //线程开辟栈的大小,为0就是默认值8M
        this.stackSize = stackSize;
        //新线程的优先级和父线程是一样的
        this.priority = currentThread.getPriority();

        this.contextClassLoader = currentThread.contextClassLoader;

        // Transfer over InheritableThreadLocals.
        if (currentThread.inheritableValues != null) {
            inheritableValues = new ThreadLocal.Values(currentThread.inheritableValues);
        }

        // add ourselves to our ThreadGroup of choice
        this.group.addThread(this);
    }

  在新建完线程之后,我们有两种方法来启动线程任务:thread.run() ; thread.start()。这两者有啥区别嘞?都是这么说的:

  thread.run() ,实际上只是在UI线程执行了Runnable的任务方法并没有实现多线程,系统也没有新开辟一个线程。

  thread.start(),才是新开一个多线程,并且在新开的线程执行Thread你们的run()方法。

  对于thread.run(),由简单的java继承机制也知道,它只是执行了Runnable的run方法,我们来看看源码吧!

 1     /**
 2      * Calls the <code>run()</code> method of the Runnable object the receiver
 3      * holds. If no Runnable is set, does nothing.
 4      *
 5      * @see Thread#start
 6      */
 7     public void run() {
 8         if (target != null) {
 9             target.run();
10         }
11     }

  由上面的create方法我们知道target就是Runnable包装在thread中的实例,还没有做任何事情,我们知道线程的新建需要请求CPU,所以直接调用run方法确实没有新建线程,只是在currentThread中直接执行了一个方法而已。我们再来看看thread.start()方法的流程又是怎么样的。

 1     /**
 2      * Starts the new Thread of execution. The <code>run()</code> method of
 3      * the receiver will be called by the receiver Thread itself (and not the
 4      * Thread calling <code>start()</code>).
 5      *
 6      * @throws IllegalThreadStateException - if this thread has already started.
 7      * @see Thread#run
 8      */
 9     public synchronized void start() {
10         checkNotStarted();
11
12         hasBeenStarted = true;
13
14         nativeCreate(this, stackSize, daemon);
15     }
16
17     private native static void nativeCreate(Thread t, long stackSize, boolean daemon);

  方法同样用synchronized关键字修饰,用来防止同一个线程阻塞。而方法的执行交给了nativeCreate方法,并且把当前Thread的实例自己传了进去,而this中就我们所知的,带了这么几个参数:

 1     volatile ThreadGroup group;
 2     volatile boolean daemon;
 3     volatile String name;
 4     volatile int priority;
 5     volatile long stackSize;
 6     Runnable target;
 7     private static int count = 0;
 8
 9     /**
10      * Holds the thread‘s ID. We simply count upwards, so
11      * each Thread has a unique ID.
12      */
13     private long id;
14
15     /**
16      * Normal thread local values.
17      */
18     ThreadLocal.Values localValues;
19
20     /**
21      * Inheritable thread local values.
22      */
23     ThreadLocal.Values inheritableValues;

  那我们只好跟过去,看看在native中到底是怎么新建的新建的线程,资源又是如何请求的(新建线程的关键)。
  顺着nativeCreate方法,我们发现它换了方法名调用的/android/art/runtime/native/java_lang_Thread.cc里面方法,名字换成了CreateNativeThread:

 1 static JNINativeMethod gMethods[] = {
 2   NATIVE_METHOD(Thread, currentThread, "!()Ljava/lang/Thread;"),
 3   NATIVE_METHOD(Thread, interrupted, "!()Z"),
 4   NATIVE_METHOD(Thread, isInterrupted, "!()Z"),
 5   NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
 6   NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
 7   NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
 8   NATIVE_METHOD(Thread, nativeInterrupt, "!()V"),
 9   NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
10   NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
11   NATIVE_METHOD(Thread, sleep, "!(Ljava/lang/Object;JI)V"),
12   NATIVE_METHOD(Thread, yield, "()V"),
13 };

  我们可以看到关于线程管理的一些方法全在里面,包括sleep、interrupted等,继续dig到CreateNativeThread的实现

 1 void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) {
 2   CHECK(java_peer != nullptr);//即为java层的thread实例,包裹着run方法的具体实现
 3   Thread* self = static_cast<JNIEnvExt*>(env)->self;
 4   Runtime* runtime = Runtime::Current();
 5
 6   // Atomically start the birth of the thread ensuring the runtime isn‘t shutting down.
 7   bool thread_start_during_shutdown = false;//这段代码用来检测thread是否在runtime宕机时start的
 8   {
 9     MutexLock mu(self, *Locks::runtime_shutdown_lock_);
10     if (runtime->IsShuttingDownLocked()) {
11       thread_start_during_shutdown = true;
12     } else {
13       runtime->StartThreadBirth();
14     }
15   }
16   if (thread_start_during_shutdown) {//若runtime宕机了就抛出异常
17     ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError"));
18     env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown");
19     return;
20   }
21
22   Thread* child_thread = new Thread(is_daemon);//新建子线程
23   // Use global JNI ref to hold peer live while child thread starts.
24   child_thread->tlsPtr_.jpeer = env->NewGlobalRef(java_peer);//把java层的run方法实体传递给子线程
25   stack_size = FixStackSize(stack_size);
26
27   // Thread.start is synchronized, so we know that nativePeer is 0, and know that we‘re not racing to
28   // assign it.
29   env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer,
30                     reinterpret_cast<jlong>(child_thread));
31
32   pthread_t new_pthread;
33   pthread_attr_t attr;
34   CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
35   CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED");
36   CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size);
37   //创建新线程的方法,返回一个标志
38   int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread);
39   CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
40
41   //线程创建如果失败则清除子线程信息,释放空间
42   if (pthread_create_result != 0) {
43     // pthread_create(3) failed, so clean up.
44     {
45       MutexLock mu(self, *Locks::runtime_shutdown_lock_);
46       runtime->EndThreadBirth();
47     }
48     // Manually delete the global reference since Thread::Init will not have been run.
49     env->DeleteGlobalRef(child_thread->tlsPtr_.jpeer);
50     child_thread->tlsPtr_.jpeer = nullptr;
51     delete child_thread;
52     child_thread = nullptr;
53     // TODO: remove from thread group?
54     env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 0);
55     {
56       std::string msg(StringPrintf("pthread_create (%s stack) failed: %s",
57                                    PrettySize(stack_size).c_str(), strerror(pthread_create_result)));
58       ScopedObjectAccess soa(env);
59       soa.Self()->ThrowOutOfMemoryError(msg.c_str());
60     }
61   }
62 }

  创建新线程在pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread)方法里由java Runtime实现,由于那里过于深入,我们就不往下挖了(往下我已经看不懂了)。但是我们有个一个创新线程过程的概念,进一步的了解了thread的id号,父线程等概念。也知道了只有通过thread.start()方法才会创建一个新的线程。
  说清了Thread和Runnable的关系,我们再来说说Handler,Handler是啥呢?当我们需要进行线程间通信的时候,我们就需要用到Handler,我们把Handler认为是一个维护消息循环队列的东西,书上都这么说,那么Handler是如何维护消息队列呢?要弄清楚这个还是得看源码。

  Google给出的关于Handler简介,大家感受下:

 1 /**
 2  * A Handler allows you to send and process {@link Message} and Runnable
 3  * objects associated with a thread‘s {@link MessageQueue}.  Each Handler
 4  * instance is associated with a single thread and that thread‘s message
 5  * queue.  When you create a new Handler, it is bound to the thread /
 6  * message queue of the thread that is creating it -- from that point on,
 7  * it will deliver messages and runnables to that message queue and execute
 8  * them as they come out of the message queue.
 9  *
10 **/

  大意是三点:

  1:Handler可以在线程的帮助下用来发送和处理Message和Runnable实例;

  2:每个Handler实例都是和单个线程和此线程的消息队列绑定的;

  3:Handler负责的工作是传送message到消息队列中且在它们从队列中出来的时候对它们进行处理。

  那么Handler是怎么维护消息队列呢?在处理消息过程中,我们常用到的几个方法:sendMessage、sendMessageAtFrontOfQueue、sendMessageAtTime、sendMessageDelayed 以及 handleMessage 。我们来看看这几个方法的源码:

 1 public final boolean sendMessage(Message msg)
 2     {
 3         return sendMessageDelayed(msg, 0);
 4     }
 5 public final boolean sendMessageDelayed(Message msg, long delayMillis)
 6     {
 7         if (delayMillis < 0) {
 8             delayMillis = 0;
 9         }
10         return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
11     }
12 public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
13         MessageQueue queue = mQueue;
14         if (queue == null) {
15             RuntimeException e = new RuntimeException(
16                     this + " sendMessageAtTime() called with no mQueue");
17             Log.w("Looper", e.getMessage(), e);
18             return false;
19         }
20         return enqueueMessage(queue, msg, uptimeMillis);
21     }
22 public final boolean sendMessageAtFrontOfQueue(Message msg) {
23         MessageQueue queue = mQueue;
24         if (queue == null) {
25             RuntimeException e = new RuntimeException(
26                 this + " sendMessageAtTime() called with no mQueue");
27             Log.w("Looper", e.getMessage(), e);
28             return false;
29         }
30         return enqueueMessage(queue, msg, 0);
31     }

  我们可以看到除了设置的不同,发送message的每个方法都实现了enqueueMessage()方法,我们看看enqueueMessage()的源码:

1     private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
2         msg.target = this;
3         if (mAsynchronous) {
4             msg.setAsynchronous(true);
5         }
6         return queue.enqueueMessage(msg, uptimeMillis);
7     }

  追到MessageQueue里面,我们可以看到一个经典的队列add的操作:

 1     boolean enqueueMessage(Message msg, long when) {
 2         if (msg.target == null) {
 3             throw new IllegalArgumentException("Message must have a target.");
 4         }
 5         if (msg.isInUse()) {
 6             throw new IllegalStateException(msg + " This message is already in use.");
 7         }
 8
 9         synchronized (this) {
10             if (mQuitting) {
11                 IllegalStateException e = new IllegalStateException(
12                         msg.target + " sending message to a Handler on a dead thread");
13                 Log.w("MessageQueue", e.getMessage(), e);
14                 msg.recycle();
15                 return false;
16             }
17
18             msg.markInUse();
19             msg.when = when;
20             Message p = mMessages;
21             boolean needWake;
22             if (p == null || when == 0 || when < p.when) {
23                 // New head, wake up the event queue if blocked.
24                 msg.next = p;
25                 mMessages = msg;
26                 needWake = mBlocked;
27             } else {
28                 // Inserted within the middle of the queue.  Usually we don‘t have to wake
29                 // up the event queue unless there is a barrier at the head of the queue
30                 // and the message is the earliest asynchronous message in the queue.
31                 needWake = mBlocked && p.target == null && msg.isAsynchronous();
32                 Message prev;
33                 //经典的队列add操作
34                 for (;;) {
35                     prev = p;
36                     p = p.next;
37                     if (p == null || when < p.when) {
38                         break;
39                     }
40                     if (needWake && p.isAsynchronous()) {
41                         needWake = false;
42                     }
43                 }
44                 msg.next = p; // invariant: p == prev.next
45                 prev.next = msg;
46             }
47
48             // We can assume mPtr != 0 because mQuitting is false.
49             if (needWake) {
50                 nativeWake(mPtr);
51             }
52         }
53         return true;
54     }

  可以看到Handler每次sendmessage时其实都是一个把message实例加到MessageQueue中的过程,无论延时还是不延时。
  发送message到消息队列的过程我们清楚了,那么Handler是怎么从消息队列中往外拿message的呢?这时候就是Looper出场的时候了,Looper也维护着一个MessageQueue,没错,这个messagequeue就是Handler发送的那个,Looper的责任就是接收消息,另一方面不停地检查messagequeue里有没有message,如果有就调用Handler中的dispatchMessage方法进行处理,我们来看看源码是怎么写的。

 1     /**
 2      * Run the message queue in this thread. Be sure to call
 3      * {@link #quit()} to end the loop.
 4      */
 5     public static void loop() {
 6         final Looper me = myLooper();
 7         if (me == null) {
 8             throw new RuntimeException("No Looper; Looper.prepare() wasn‘t called on this thread.");
 9         }
10         final MessageQueue queue = me.mQueue;
11
12         // Make sure the identity of this thread is that of the local process,
13         // and keep track of what that identity token actually is.
14         Binder.clearCallingIdentity();
15         final long ident = Binder.clearCallingIdentity();
16
17         for (;;) {
18             Message msg = queue.next(); // might block
19             if (msg == null) {
20                 // No message indicates that the message queue is quitting.
21                 return;
22             }
23
24             // This must be in a local variable, in case a UI event sets the logger
25             Printer logging = me.mLogging;
26             if (logging != null) {
27                 logging.println(">>>>> Dispatching to " + msg.target + " " +
28                         msg.callback + ": " + msg.what);
29             }
30
31             msg.target.dispatchMessage(msg);
32
33             if (logging != null) {
34                 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
35             }
36
37             // Make sure that during the course of dispatching the
38             // identity of the thread wasn‘t corrupted.
39             final long newIdent = Binder.clearCallingIdentity();
40             if (ident != newIdent) {
41                 Log.wtf(TAG, "Thread identity changed from 0x"
42                         + Long.toHexString(ident) + " to 0x"
43                         + Long.toHexString(newIdent) + " while dispatching to "
44                         + msg.target.getClass().getName() + " "
45                         + msg.callback + " what=" + msg.what);
46             }
47
48             msg.recycleUnchecked();
49         }
50     }

  我们能看到有一句处理消息的代码: msg.target.dispatchMessage(msg);

  再看看enqueueMessage方法里:msg.target = this;

  对的,message的target就是handler:

 1    /**
 2      * Handle system messages here.
 3      */
 4     public void dispatchMessage(Message msg) {
 5         if (msg.callback != null) {
 6             handleCallback(msg);
 7         } else {
 8             if (mCallback != null) {
 9                 if (mCallback.handleMessage(msg)) {
10                     return;
11                 }
12             }
13             handleMessage(msg);
14         }
15     }

  一切的一切都回到了handleMessage()方法中,我们最熟悉的方法。
  我们用一张流程图来总结一下Handler、Thread和Runnable三者的关系。

时间: 2024-10-11 15:02:32

Handler、Thread和Runnable简单分析的相关文章

安卓线程相关 HandlerThread Handler Thread Looper Message Runnable

本文由PurpleSword(jzj1993)原创,转载请注明 原文网址 http://blog.csdn.net/jzj1993 安卓主线程(UI线程)是线程不安全的:对UI控件的操作都应在主线程中完成:UI线程不应执行耗时操作,以免程序不响应(即ANR异常) 实现新线程的常用方法(注意要调用start方法启动新线程而不是run方法): 一.定义类,实现Runnable接口 class MyRunnable implements Runnable { /** * 实现接口的run方法 */ @

Handler Thread 内部类引起内存泄露分析

非静态内部类引起内存泄漏的原因 内部类的实现其实是通过编译器的语法糖(Syntactic sugar)实现的,通过生成相应的子类即以OutClassName$InteriorClassName命名的Class文件.并添加构造函数,在构造函数中[传入]外部类,这也是为什么内部类能使用外部类的方法与字段的原因.所以,当外部类与内部类生命周期不一致的时候很有可能发生内存泄漏. Handler引起内存泄漏案例分析 例如,当使用内部类(包括匿名类)来创建Handler的时候,Handler对象会隐式地持有

Android中Handler 、Thread和Runnable之间的关系

在多线程编程的时候,我们经常会用到Handler,Thread和Runnable这三个类,我们来看看这三个类之间是怎么样的关系? 首先说明Android的CPU分配的最小单元是线程,Handler一般是在某个线程里创建的,因而Handler和Thread就是相互绑定的,一一对应. 而Runnable是一个接口,Thread是Runnable的子类.可以说,他俩都算一个进程. HandlerThread顾名思义就是可以处理消息循环的线程,他是一个拥有Looper的线程,可以处理消息循环. 与其说H

多线程-Thread与Runnable源码分析

Runnable: @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 call

【Android】Handler、Looper源码分析

一.前言 源码分析使用的版本是 4.4.2_r1. Handler和Looper的入门知识以及讲解可以参考我的另外一篇博客:Android Handler机制 简单而言:Handler和Looper是对某一个线程实现消息机制的重要组成部分,另外两个重要元素是Message和MessageQueue,通过这四个类,可以让某个线程具备接收.处理消息的能力. 二.源码剖析 虽然只有四个类,而且这里只是剖析其中两个,但是也不能独立分析,必须组合进行解析.切入点是类Looper的注释中的一段示例代码: 1

Android进阶——多线程系列之Thread、Runnable、Callable、Future、FutureTask

多线程系列之Thread.Runnable.Callable.Future.FutureTask 前言 多线程一直是初学者最抵触的东西,如果你想进阶的话,那必须闯过这道难关,特别是多线程中Thread.Runnable.Callable.Future.FutureTask这几个类往往是初学者容易搞混的.这里先总结这几个类特点和区别,让大家带着模糊印象来学习这篇文章 Thread.Runnable.Callable:都是线程 Thread特点:提供了线程等待.线程睡眠.线程礼让等操作 Runnab

ImageLoader简单分析(三)

其实对于缓存的实现原理及其流程总的来说都很简单,无非就是先从网络加载相关资源,然后用内存缓存或者磁盘缓存把下载到的资源缓存起来:等再次加载相同的资源的时候如果内存缓存或者磁盘缓存还存在就用缓存里面的资源,否则仍然进行网络加载,重复此过程而已.严格说来也没什么可讲的,但是通过研读ImageLoader的源码倒是可以学到很多缓存之外的东西:学学别人的代码怎么设计,资源加载的异步处理机制的灵活使用等等,甚至也可以吸取到一些东西来运用到自己的项目中去.就ImageLoader本身来说,也可以让人了解其工

x264源代码简单分析:x264命令行工具(x264.exe)

本文简单分析x264项目中的命令行工具(x264.exe)的源代码.该命令行工具可以调用libx264将YUV格式像素数据编码为H.264码流. 函数调用关系图 X264命令行工具的源代码在x264中的位置如下图所示. 单击查看更清晰的图片 X264命令行工具的源代码的调用关系如下图所示. 单击查看更清晰的图片 从图中可以看出,X264命令行工具调用了libx264的几个API完成了H.264编码工作.使用libx264的API进行编码可以参考<最简单的视频编码器:基于libx264(编码YUV

ImageLoader的简单分析(二)

在<ImageLoader的简单分析>这篇博客中对IImageLoader三大组件的创建过程以及三者之间的关系做了说明,同时文章的最后也简单的说明了一下ImageLoader是怎么通过displayImage方法来获取缓存来显示图片的,本文就对ImageLoader的这个知识点做较为详细的说明. ImageLoader对缓存的使用还是很灵活的:支持同步或者异步加载图片资源,对内存缓存和文件缓存可以选择使用其一或者二者都是用.在构建DisplayImageOptions对象的时候可以通过cach