在前文《Android多线程分析之一:使用Thread异步下载图像》中演示了如何使用 Thread 处理异步事务。示例中这个 Java Thread 类都是位于 Framework 层的类,它自身是通过 JNI 转调 dalvik 里面的 Thread 相关方法实现的。因此要分析 Androd 中的线程,就需要分析这两层中的与线程相关的代码,这就是本文要探讨的主题。本文将把 Framework 层中的 Java Thread 称为 Android 线程/Thread,而把 dalvik 中的 Thread 成为 dalvik 线程/Thread。
本文涉及到的 Android 源码路径:
android/libcore/luni/src/main/java/java/lang/Runnable.java
android/libcore/luni/src/main/java/java/lang/Thread.java
android/libcore/luni/src/main/java/java/lang/ThreadGroup.java
android/libcore/luni/src/main/java/java/lang/VMThread.java
android/dalvik/vm/native/java_lang_VMThread.cpp
android/dalvik/vm/Thread.cpp
首先来分析 Android Thread,这个类的源码在android/libcore/luni/src/main/java/java/lang/Thread.java,它实现了 Runnable 接口。Runnable 只有一个无参无返回值的 void run() 的接口:
/** * Represents a command that can be executed. Often used to run code in a * different {@link Thread}. */ public interface Runnable { /** * Starts executing the active part of the class' code. This method is * called when a thread is started that has been created with a class which * implements {@code Runnable}. */ public void run(); }
/** * A representation of a thread's state. A given thread may only be in one * state at a time. */ public enum State { /** * The thread has been created, but has never been started. */ NEW, /** * The thread may be run. */ RUNNABLE, /** * The thread is blocked and waiting for a lock. */ BLOCKED, /** * The thread is waiting. */ WAITING, /** * The thread is waiting for a specified amount of time. */ TIMED_WAITING, /** * The thread has been terminated. */ TERMINATED }
volatile VMThread vmThread; volatile ThreadGroup group; volatile boolean daemon; volatile String name; volatile int priority; volatile long stackSize; Runnable target; private static int count = 0; private long id; ThreadLocal.Values localValues;
vmThread:可视为对 dalvik thread 的简单封装,Thread 类通过 VMThread 里面的 JNI 方法来调用 dalvik 中操作线程的方法,通过它的成员变量 thread 和 vmata,我们可以将 Android Thread 和 dalvik Thread 的关联起来;
group:每一个线程都属于一个group,当线程被创建时就会加入一个特定的group,当线程运行结束,会从这个 group 中移除;
daemon:当前线程是不是守护线程,守护线程只会在没有非守护线程运行的情况下才会运行;
priority:线程优先级,Java Thread 类的线程优先级取值范围为 [1, 10],默认优先级为 5;
stackSize:线程栈大小,默认为 0,即使用默认的线程栈大小(由 dalvik 中的全局变量 gDvm.stackSize 决定);
target:一个 Runnable 对象,Thread 的 run() 方法中会转调该 target 的 run() 方法,这是线程真正处理事务的地方;
id:线程 id,通过递增 count 得到该id,如果没有显式给线程设置名字,那么就会使用 Thread+id 当作线程的名字。注意这不是真正意义上的线程 id,即在 logcat 中打印的 tid 并不是这个 id,那 tid 是指 dalvik 线程的 id;
localValues:线程本地存储(TLS)数据;
接下来,我们来看Android Thread 的构造函数,大部分构造函数都是通过转调静态函数 create 实现的,下面来详细分析 create 这个关键函数:
private void create(ThreadGroup group, Runnable runnable, String threadName, long stackSize) { Thread currentThread = Thread.currentThread(); if (group == null) { group = currentThread.getThreadGroup(); } if (group.isDestroyed()) { throw new IllegalThreadStateException("Group already destroyed"); } this.group = group; synchronized (Thread.class) { id = ++Thread.count; } if (threadName == null) { this.name = "Thread-" + id; } else { this.name = threadName; } this.target = runnable; 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); }
public static Thread currentThread() { return VMThread.currentThread(); }
static void Dalvik_java_lang_VMThread_currentThread(const u4* args, JValue* pResult) { UNUSED_PARAMETER(args); RETURN_PTR(dvmThreadSelf()->threadObj); }
Thread* dvmThreadSelf() { return (Thread*) pthread_getspecific(gDvm.pthreadKeySelf); }
/* the java/lang/Thread that we are associated with */ Object* threadObj;
接着分析上面的代码,如果没有给新线程指定 group 那么就会指定 group 为当前线程所在的 group 中,然后给新线程设置 name,priority 等。最后通过调用 ThreadGroup 的 addThread 方法将新线程添加到 group 中:
/** * Called by the Thread constructor. */ final void addThread(Thread thread) throws IllegalThreadStateException { synchronized (threadRefs) { if (isDestroyed) { throw new IllegalThreadStateException(); } threadRefs.add(new WeakReference<Thread>(thread)); } }
上面分析的是 Android Thread 的构造过程,从上面的分析可以看出,Android Thread 的构造方法仅仅是设置了一些线程属性,并没有真正去创建一个新的 dalvik Thread,dalvik Thread 创建过程要等到客户代码调用 Android Thread 的 start() 方法才会进行。下面我们来分析 Java Thread 的 start() 方法:
public synchronized void start() { if (hasBeenStarted) { throw new IllegalThreadStateException("Thread already started."); // TODO Externalize? } hasBeenStarted = true; VMThread.create(this, stackSize); } }
static void Dalvik_java_lang_VMThread_create(const u4* args, JValue* pResult) { Object* threadObj = (Object*) args[0]; s8 stackSize = GET_ARG_LONG(args, 1); /* copying collector will pin threadObj for us since it was an argument */ dvmCreateInterpThread(threadObj, (int) stackSize); RETURN_VOID(); }
bool dvmCreateInterpThread(Object* threadObj, int reqStackSize) { Thread* self = dvmThreadSelf(); ... Thread* newThread = allocThread(stackSize); newThread->threadObj = threadObj; ... Object* vmThreadObj = dvmAllocObject(gDvm.classJavaLangVMThread, ALLOC_DEFAULT); dvmSetFieldInt(vmThreadObj, gDvm.offJavaLangVMThread_vmData, (u4)newThread); dvmSetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread, vmThreadObj); ... pthread_t threadHandle; int cc = pthread_create(&threadHandle, &threadAttr, interpThreadStart, newThread); /* * Tell the new thread to start. * * We must hold the thread list lock before messing with another thread. * In the general case we would also need to verify that newThread was * still in the thread list, but in our case the thread has not started * executing user code and therefore has not had a chance to exit. * * We move it to VMWAIT, and it then shifts itself to RUNNING, which * comes with a suspend-pending check. */ dvmLockThreadList(self); assert(newThread->status == THREAD_STARTING); newThread->status = THREAD_VMWAIT; pthread_cond_broadcast(&gDvm.threadStartCond); dvmUnlockThreadList(); ... } /* * Alloc and initialize a Thread struct. * * Does not create any objects, just stuff on the system (malloc) heap. */ static Thread* allocThread(int interpStackSize) { Thread* thread; thread = (Thread*) calloc(1, sizeof(Thread)); ... thread->status = THREAD_INITIALIZING; }
然后,通过 pthread_create 创建 pthread 线程,并让这个线程 start,这样就会进入该线程的 thread entry 运行,下来我们来看新线程的 thread entry 方法 interpThreadStart,同样只列出关键的地方:
/* * pthread entry function for threads started from interpreted code. */ static void* interpThreadStart(void* arg) { Thread* self = (Thread*) arg; std::string threadName(dvmGetThreadName(self)); setThreadName(threadName.c_str()); /* * Finish initializing the Thread struct. */ dvmLockThreadList(self); prepareThread(self); while (self->status != THREAD_VMWAIT) pthread_cond_wait(&gDvm.threadStartCond, &gDvm.threadListLock); dvmUnlockThreadList(); /* * Add a JNI context. */ self->jniEnv = dvmCreateJNIEnv(self); /* * Change our state so the GC will wait for us from now on. If a GC is * in progress this call will suspend us. */ dvmChangeStatus(self, THREAD_RUNNING); /* * Execute the "run" method. * * At this point our stack is empty, so somebody who comes looking for * stack traces right now won't have much to look at. This is normal. */ Method* run = self->threadObj->clazz->vtable[gDvm.voffJavaLangThread_run]; JValue unused; ALOGV("threadid=%d: calling run()", self->threadId); assert(strcmp(run->name, "run") == 0); dvmCallMethod(self, run, self->threadObj, &unused); ALOGV("threadid=%d: exiting", self->threadId); /* * Remove the thread from various lists, report its death, and free * its resources. */ dvmDetachCurrentThread(); return NULL; } /* * Finish initialization of a Thread struct. * * This must be called while executing in the new thread, but before the * thread is added to the thread list. * * NOTE: The threadListLock must be held by the caller (needed for * assignThreadId()). */ static bool prepareThread(Thread* thread) { assignThreadId(thread); thread->handle = pthread_self(); thread->systemTid = dvmGetSysThreadId(); setThreadSelf(thread); ... return true; } /* * Explore our sense of self. Stuffs the thread pointer into TLS. */ static void setThreadSelf(Thread* thread) { int cc; cc = pthread_setspecific(gDvm.pthreadKeySelf, thread); ... }
public void run() { if (target != null) { target.run(); } }
对于继承自 Android Thread 带有 Looper 的 Android HandlerThread 来说,会调用它覆写 run 方法():(关于 Looper 的话题下一篇会讲到,这里暂且略过)
public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; }
/* * Detach the thread from the various data structures, notify other threads * that are waiting to "join" it, and free up all heap-allocated storage. * / void dvmDetachCurrentThread() { Thread* self = dvmThreadSelf(); Object* vmThread; Object* group; ... group = dvmGetFieldObject(self->threadObj, gDvm.offJavaLangThread_group); /* * Remove the thread from the thread group. */ if (group != NULL) { Method* removeThread = group->clazz->vtable[gDvm.voffJavaLangThreadGroup_removeThread]; JValue unused; dvmCallMethod(self, removeThread, group, &unused, self->threadObj); } /* * Clear the vmThread reference in the Thread object. Interpreted code * will now see that this Thread is not running. As this may be the * only reference to the VMThread object that the VM knows about, we * have to create an internal reference to it first. */ vmThread = dvmGetFieldObject(self->threadObj, gDvm.offJavaLangThread_vmThread); dvmAddTrackedAlloc(vmThread, self); dvmSetFieldObject(self->threadObj, gDvm.offJavaLangThread_vmThread, NULL); /* clear out our struct Thread pointer, since it's going away */ dvmSetFieldObject(vmThread, gDvm.offJavaLangVMThread_vmData, NULL); ... /* * Thread.join() is implemented as an Object.wait() on the VMThread * object. Signal anyone who is waiting. */ dvmLockObject(self, vmThread); dvmObjectNotifyAll(self, vmThread); dvmUnlockObject(self, vmThread); dvmReleaseTrackedAlloc(vmThread, self); vmThread = NULL; ... dvmLockThreadList(self); /* * Lose the JNI context. */ dvmDestroyJNIEnv(self->jniEnv); self->jniEnv = NULL; self->status = THREAD_ZOMBIE; /* * Remove ourselves from the internal thread list. */ unlinkThread(self); ... releaseThreadId(self); dvmUnlockThreadList(); setThreadSelf(NULL); freeThread(self); } /* * Free a Thread struct, and all the stuff allocated within. */ static void freeThread(Thread* thread) { ... free(thread); }