欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android關(guān)于native中Thread類的使用源碼解析

 更新時(shí)間:2025年06月20日 09:23:44   作者:開發(fā)之奮斗人生  
本文介紹了Android Native中Thread的使用,通過main.cpp啟動(dòng)線程,TestThread類依次調(diào)用onFirstRef、readyToRun和threadLoop方法,展示線程生命周期及執(zhí)行流程,感興趣的朋友跟隨小編一起看看吧

簡要概述

簡單記錄android中native關(guān)于thread的使用
源碼位置:

system\core\libutils\include\utils\Thread.h
system\core\libutils\Threads.cpp
class Thread : virtual public RefBase
Thread繼承RefBase,有以下的一些特性
// Invoked after creation of initial strong pointer/reference.
virtual void            onFirstRef();

代碼記錄

main.cpp

#include <utils/Log.h>
#include <pthread.h>
#include "TestThread.h"
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "hello_test"
using namespace android;
int main(int args,char** argv) {
    ALOGD("main start TestThread");
    // TestThread
    sp<TestThread> testThread = new TestThread;
    testThread->run("TestThread", PRIORITY_URGENT_DISPLAY);
    while(1){
        if(!testThread->isRunning()){
            break;
        }
    }
     ALOGD("main end");
    return 0;
}

Android.bp

cc_binary{
    name:"hello_test",
    srcs:[
        "main.cpp",
        "TestThread.cpp",
    ],
    shared_libs:[
        "liblog",
        "libutils",
    ],
    cflags: [
            "-Wno-error",
            "-Wno-unused-parameter",
        ],
}

TestThread.h

//
// Created by xxx on 25-6-8.
//
#ifndef ANDROID_TESTTHREAD_H
#define ANDROID_TESTTHREAD_H
#include <utils/threads.h>
#include <utils/Log.h>
namespace android {
    class TestThread : public Thread {
        public:
            TestThread();
            virtual void        onFirstRef();
            virtual status_t    readyToRun();
            virtual bool        threadLoop();
            virtual void        requestExit();
        private:
            int cnt = 0;
    };
}
#endif //ANDROID_TESTTHREAD_H

TestThread.cpp

//
// Created by xxx on 25-6-8.
//
#include "TestThread.h"
namespace android{
    TestThread::TestThread():Thread(false) {
        ALOGD("TestThread");
    }
    void TestThread::onFirstRef(){
        ALOGD("onFirstRef");
    }
    status_t  TestThread::readyToRun(){
        ALOGD("readyToRun");
        return OK;
    }
    bool TestThread::threadLoop() {
        cnt++;
        ALOGD("threadLoop cnt = %d",cnt);
        if(cnt >= 20){
           return false;
        }
        return true;
    }
    void  TestThread::requestExit(){
        ALOGD("requestExit");
    }
}

日志打印如下所示

06-14 22:29:28.500  2094  2094 D hello_test: main start TestThread
06-14 22:29:28.500  2094  2094 D hello_test: TestThread
06-14 22:29:28.500  2094  2094 D hello_test: onFirstRef
06-14 22:29:28.501  2094  2096 D hello_test: readyToRun
06-14 22:29:28.502  2094  2096 D hello_test: threadLoop cnt = 1
...
06-14 22:29:28.505  2094  2096 D hello_test: threadLoop cnt = 20
06-14 22:29:28.505  2094  2094 D hello_test: main end

函數(shù)執(zhí)行順序 TestThread->onFirstRef->readyToRun->threadLoop

到此這篇關(guān)于android關(guān)于native中Thread類的使用的文章就介紹到這了,更多相關(guān)android native Thread類使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論