class AbcData {
public:
AbcData();
void addCout();
int getCout();
private:
Condition mWorkChangedCondition;
Mutex mLock;
int cout;
};
AbcData::AbcData(){
cout =0;
}
int AbcData::getCout(){
mWorkChangedCondition.wait(mLock);
return cout;
}
void AbcData::addCout(){
cout++;
ALOGD("addCout %d \n",cout);
if(cout%20 ==0){
ALOGD("cout%20 ==0");
mWorkChangedCondition.broadcast();
}
}
class AbcThread: public Thread
{
public:
AbcThread();
virtual ~AbcThread();
void addCout();
protected:
virtual bool threadLoop(); // Explain later for the return value
AbcData mAbcData;
};
void AbcThread::addCout(){
mAbcData.addCout();
}
AbcThread::AbcThread():Thread(false) // Thread inherit from RefBase.
{
ALOGD("Consctruct");
}
AbcThread::~AbcThread()
{
ALOGD("Desctruct");
}
bool AbcThread::threadLoop()
{
// Put what you want to do when thread is running here...
ALOGD("ThreadLoop()");
while(true){
int cout =mAbcData.getCout();
ALOGD("ThreadLoop() wait release %d \n",cout);
}
return true;
}
调用
1开启
mAbcthread = new AbcThread; // Note: Must write like this!
mAbcthread->run("AbcThread"); // thread->run() is fine too
2刷新
mAbcthread->addCout();