Android对Thread的C++封装

Android对Thread的C++封装,见源码/frameworks/base/include/utils/threads.h

 

#define LOG_TAG "MyThread"

#include <utils/Log.h>
#include <utils/threads.h>
using namespace android;

class MyThread: public Thread {
public:
    MyThread() {
    LOGD("MyThread");
    };

    virtual ~MyThread(){
    LOGD("~MyThread");
    };
      
    //如果返回true,循环调用此函数,返回false下一次不会再调用此函数
    bool threadLoop(){
      LOGD("threadLoop");
      return true;
    };

};

int main(){
  LOGD("main start");
  sp<MyThread> th = new MyThread();  //正确的使用方法;
  //MyThread* th = new MyThread();   //错误的使用方法;
  th->run();//和java不一样哦
  while(1);
  LOGD("main end");
  return 0;
}

 

posted @ 2015-04-16 16:24  牧 天  阅读(1314)  评论(0)    收藏  举报