线程在wait()和notify()之后丢失消息
我在处理线程中的消息时遇到问题。我的运行方法看起来像这样
public void run() {
Looper.prepareLooper();
parserHandler = new Handler {
public void handleMessage(Message msg) {
Log.i("","id from message: "+msg.getData.getString("id"));
// handle message
this.wait();
}
}
}
我有几个活动向该线程发送消息,如下所示:
Message parserMessage = new Message();
Bundle data = new Bundle();
data.putString("id", realId);
data.putString("callingClass", "CategoryList");
parserMessage.setData(data);
parserMessage.what = PARSE_CATEGORIES_OR_PRODUCTS;
parserHandler = parser.getParserHandler();
synchronized (parserHandler) {
parserHandler.notify();
Log.i("","message ID: " + parserMessage.getData().getString("id"));
}
parserHandler.sendMessage(parserMessage);
问题是运行方法记录“id from message: null”,尽管“message ID”在日志语句中具有值。为什么消息在发送到线程时会“丢失”其数据?跟通知有关系吗?感谢您的帮助
I have a problem handling messages in a Thread. My run-method looks like this
public void run() {
Looper.prepareLooper();
parserHandler = new Handler {
public void handleMessage(Message msg) {
Log.i("","id from message: "+msg.getData.getString("id"));
// handle message
this.wait();
}
}
}
I have several Activities sending messages to this thread, like this:
Message parserMessage = new Message();
Bundle data = new Bundle();
data.putString("id", realId);
data.putString("callingClass", "CategoryList");
parserMessage.setData(data);
parserMessage.what = PARSE_CATEGORIES_OR_PRODUCTS;
parserHandler = parser.getParserHandler();
synchronized (parserHandler) {
parserHandler.notify();
Log.i("","message ID: " + parserMessage.getData().getString("id"));
}
parserHandler.sendMessage(parserMessage);
The problem is that the run-method logs "id from message: null" though "message ID" has a value in the Log-statement. Why does the message "lose" it's data when being send to the thread? Has it something to do with the notify? Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我想我已经解决了这个问题。它是 MessageQueue 与我在代码的其他位置使用的 OnScrollListener 的结合。 onScrollListener 被多次调用,因此 MessageQueue 被来自该侦听器的消息阻塞。
So I think I figured out the problem. It was the MessageQueu in combination with an OnScrollListener I used in an other location of my code. The onScrollListener was called multiple times and so the MessageQueu was blocked with messages from this listener.