android thread使用测试

来源:互联网 发布:知行理工登录密码多少 编辑:程序博客网 时间:2024/05/15 02:05

本文主要是想测试 threadLoop 线程是不是循环执行。


1. ThreadTest.cpp

#include <binder/IPCThreadState.h>#include <binder/ProcessState.h>#include <utils/threads.h>namespace android {    class ThreadTest : public Thread {        virtual bool threadLoop();    };    bool ThreadTest::threadLoop() {        printf("threadLoop\n");        sleep(1);        return true;    }}using namespace android;int main() {        sp<ThreadTest> thr = new ThreadTest();    thr->run();    ProcessState::self()->startThreadPool();    IPCThreadState::self()->joinThreadPool();    return 0;}

2. Android.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS:= optionalLOCAL_SRC_FILES := \    ThreadTest.cppLOCAL_SHARED_LIBRARIES := \    libutils \    libbinderLOCAL_MODULE := ThreadTestinclude $(BUILD_EXECUTABLE)

3. 执行

可以看到每隔1秒,打印一次threadLoop,所以threadLoop是循环执行。

若用指针代替android的sp,即StrongPointer,则只打印一次threadLoop。如下所示:

//sp<ThreadTest> thr = new ThreadTest();
ThreadTest *thr = new ThreadTest();

或者在threadLoop函数中return false,也只打印一次threadLoop。


原因是Thread类的实现中,

先由pthreads或win32_threads实现创建thread,并将线程入口设为_threadLoop。

_threadLoop再调用thread类实现者(ThreadTest)的threadLoop,并将这个threadLoop

放在do-while循环中,只有当threadLoop返回false或线程对象指针不是sp实现时,会退出循环。

_threadLoop示意代码:

int Thread::_threadLoop(void* user){    Thread* const self = static_cast<Thread*>(user);    sp<Thread> strong(self->mHoldSelf);    wp<Thread> weak(strong);    self->mHoldSelf.clear();#ifdef HAVE_ANDROID_OS    // this is very useful for debugging with gdb    self->mTid = gettid();#endif    bool first = true;    do {        bool result;        if (first) {            first = false;            self->mStatus = self->readyToRun();            result = (self->mStatus == NO_ERROR);            if (result && !self->exitPending()) {                // Binder threads (and maybe others) rely on threadLoop                // running at least once after a successful ::readyToRun()                // (unless, of course, the thread has already been asked to exit                // at that point).                // This is because threads are essentially used like this:                //   (new ThreadSubclass())->run();                // The caller therefore does not retain a strong reference to                // the thread and the thread would simply disappear after the                // successful ::readyToRun() call instead of entering the                // threadLoop at least once.                result = self->threadLoop();            }        } else {            result = self->threadLoop();        }        // establish a scope for mLock        {        Mutex::Autolock _l(self->mLock);        if (result == false || self->mExitPending) {            self->mExitPending = true;            self->mRunning = false;            // clear thread ID so that requestExitAndWait() does not exit if            // called by a new thread using the same thread ID as this one.            self->mThread = thread_id_t(-1);            // note that interested observers blocked in requestExitAndWait are            // awoken by broadcast, but blocked on mLock until break exits scope            self->mThreadExitedCondition.broadcast();            break;        }        }                // Release our strong reference, to let a chance to the thread        // to die a peaceful death.        strong.clear();        // And immediately, re-acquire a strong reference for the next loop        strong = weak.promote();    } while(strong != 0);        return 0;}

关于sp,wp,可参考:

深入理解android常用类