android4.2.2中Thread类的头文件定义已经移到下面目录:
frameworks\native\include\utils\thread.h
class Thread : virtual public RefBase
{
public:
// Create a Thread object, but doesn't create or start the associated
// thread. See the run() method.
Thread(bool canCallJava = true);
virtual ~Thread();
// Start the thread in threadLoop() which needs to be implemented.
virtual status_t run( const char* name = 0,
int32_t priority = PRIORITY_DEFAULT,
size_t stack = 0);
// Ask this object's thread to exit. This function is asynchronous, when the
// function returns the thread might still be running. Of course, this
// function can be called from a different thread.
virtual void requestExit();
// Good place to do one-time initializations
virtual status_t readyToRun();
// Call requestExit() and wait until this object's thread exits.
// BE VERY CAREFUL of deadlocks. In particular, it would be silly to call
// this function from this object's thread. Will return WOULD_BLOCK in
// that case.
status_t requestExitAndWait();
// Wait until this object's thread exits. Returns immediately if not yet running.
// Do not call from this object's thread; will return WOULD_BLOCK in that case.
status_t join();
#ifdef HAVE_ANDROID_OS
// Return the thread's kernel ID, same as the thread itself calling gettid() or
// androidGetTid(), or -1 if the thread is not running.
pid_t getTid() const;
#endif
protected:
// exitPending() returns true if requestExit() has been called.
bool exitPending() const;
private:
// Derived class must implement threadLoop(). The thread starts its life
// here. There are two ways of using the Thread object:
// 1) loop: if threadLoop() returns true, it will be called again if
// requestExit() wasn't called.
// 2) once: if threadLoop() returns false, the thread will exit upon return.
virtual bool threadLoop() = 0;
private:
Thread& operator=(const Thread&);
static int _threadLoop(void* user);
const bool mCanCallJava;
// always hold mLock when reading or writing
thread_id_t mThread;
mutable Mutex mLock;
Condition mThreadExitedCondition;
status_t mStatus;
// note that all accesses of mExitPending and mRunning need to hold mLock
volatile bool mExitPending;
volatile bool mRunning;
sp<Thread> mHoldSelf;
#ifdef HAVE_ANDROID_OS
// legacy for debugging, not used by getTid() as it is set by the child thread
// and so is not initialized until the child reaches that point
pid_t mTid;
#endif
};
Thread类函数说明在这个头文件中已经描述的很清楚,这里主要介绍下如何使用这个类。通常我们要在native代码中使用线程都是定义一个自己的线程类并继承自Thread,在自己定义的线程类中需要实现threadLoop方法,通过run函数来启动线程。主要涉及的几个方法的说明如下:
virtual status_t run( const char* name = 0,
int32_t priority = PRIORITY_DEFAULT,
size_t stack = 0);
创建一个thread实例的时候,线程并没有立即运行,需要调用run函数来启动线程。
virtual status_t readyToRun();
这个函数定义thread执行前的初始化工作,在继承类中可以实现这个函数。
virtual bool threadLoop() = 0;
这个函数是主线程执行函数,每个继承类都需要实现它。这个函数如果返回true,则函数会不停地执行threadloop中的内容,如果这个函数返回false,则threadloop中的内容仅仅执行一次线程就会退出。
一个简单的例子:
1)定义线程类
class MyThread : public Thread {
int test;
public:
MyThread(int i) :
Thread(false),
test(i) {
}
void startThread() {
run("MyThread", PRIORITY_URGENT_DISPLAY);
}
void stopThread() {
requestExitAndWait();
}
virtual bool threadLoop();
};
2)实现线程类bool MyThread::threadLoop() {
LOGD("threadLoop: %d.", test++);
return true; //如果返回true,循环调用此函数,返回false下一次不会再调用此函数.
}
3)启动线程
sp<MyThread> mMyThread;
mMyThread = new MyThread(this);
mMyThread->startThread();