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常用类
- android thread使用测试
- Android Thread的使用
- Android:使用Service还是Thread
- android 何时使用Service 何时使用Thread
- Android 使用Handler实现Thread间通信
- android handler和thread的使用
- android 使用Thread的一种方法-HandlerThread
- Android 使用Handler实现Thread间通信
- Android 使用Handler实现Thread间通信
- 08 Android 使用Thread操作UI
- Android异步处理:使用Thread+Hand…
- Android-------Handler,Thread,Message使用范例
- android异步处理Handler+Thread使用进阶
- android handler和thread的使用
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android中Timer与Thread的使用
- 内建的嵌套向量中断控制器
- 【技术类】【遥感知识】植被波谱特征总结
- 开机自启动nginx
- Ubuntu Linux常见中文输入法汇总
- linux下取log4j生成的某段时间内的日志记录
- android thread使用测试
- VGA DVI HDMI区别
- 一个java程序员的情书
- Git使用指南
- 【在线应用】影像监测应用系统 “预告片”
- iOS6新特性
- IOS开发者证书配置和安装。。。
- xml sax localName和qName的区别
- 用vector<CStringArray>会报错;数组(一维、二维、三维)的动态申请及用vector的表示方法