jade代码笔记 receive(pattern)

receive(MessageTemplate pattern)是一个线程安全的方法,对msgQueue做同步,即一次只有一个receive访问msgQueue,遍历队列,直到找到与pattern匹配的消息,先保存当前消息,再从队列中删除,并告知agenttoolkit已得到消息(MIDP与手机有关),再返回这个消息。
public final ACLMessage receive(MessageTemplate pattern) {
ACLMessage msg = null;
synchronized (msgQueue) {
for (Iterator messages = msgQueue.iterator(); messages.hasNext(); ) {
final ACLMessage cursor = (ACLMessage)messages.next();
if (pattern == null || pattern.match(cursor)) {
try {
//messages.remove();
msgQueue.remove(cursor);
//#MIDP_EXCLUDE_BEGIN
notifyReceived(cursor);
//#MIDP_EXCLUDE_END
currentMessage = cursor;
msg = cursor;
break; // Exit while loop
}
catch (Exception e) {
e.printStackTrace();
// Continue loop, discard message
}
}
}
}
return msg;
}

你可能感兴趣的:(Pattern)