Create 同步处理消息.md

master
Omooo 5 years ago
parent d066d53e48
commit c70e218458
  1. 59
      blogs/Android/Framework/Interview/技巧心得相关/同步处理消息.md

@ -0,0 +1,59 @@
---
怎么同步处理消息?
---
```c++
status_t Client::createSurface(const String&& name, ...){
sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), name, this, w, h, format, flags, handle, gbp);
mFlinger->postMessageSync(msg);
return static_cast<MessageCreateLayer*>(msg.get())->getResult();
}
status_t postMessageSync(const sp<MessageBase>& msg, ...){
mEventQueue.postMessage(msg, reltime);
msg.wait();
}
class MessageCreateLayer:public MessageBase{
status_t getResult() const{return result;}
virtual bool handler(){
result = flinger->createLayer(name, client,w, h, format, ...);
return true;
}
}
```
```java
public final boolean runWithScissors(final Runnable r, long timeout){
if(Looper.myLooper()==mLooper){
r.run();
return true;
}
BlockingRunnable br = new BlockingRunnable(r);
return br.postAndWait(this, timeout);
}
private static final class BlockingRunnable implements Runnable{
public BlockingRunnable(Runnable task){
mTask = task;
}
@Override
public void run(){
mTask.run();
mDone = true;
notifyAll();
}
}
public boolean postAndWait(Handler handler, ...){
if(!handler.post(this)){
return false;
}
while(!mDone){
wait();
}
return true;
}
```
#### 总结
1. 同步等待消息的处理
2. Binder 调用统一切换工作线程
Loading…
Cancel
Save