android關(guān)于native中Thread類的使用源碼解析
簡要概述
簡單記錄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_HTestThread.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)文章
Android性能優(yōu)化getResources()與Binder導(dǎo)致界面卡頓優(yōu)化
這篇文章主要為大家介紹了Android性能優(yōu)化getResources()與Binder導(dǎo)致界面卡頓優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Android TextView設(shè)置背景色與邊框的方法詳解
本篇文章是對Android中TextView設(shè)置背景色與邊框的方法進行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android屏幕適配工具類 Android自動生成不同分辨率的值
這篇文章主要為大家詳細(xì)介紹了Android屏幕適配工具類,Android自動生成不同分辨率的值,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03

