我有以下服务代码,我在其中启动了一个线程,负责在消息传入时分派消息。
public void run() {
while (! Thread.interrupted()) {
try {
Message msg = null;
synchronized (_queue) {
if (_queue.size() == 0) {
_queue.wait(10000);
}
if (_queue.size() != 0) {
msg = _queue.poll();
}
if (msg != null) {
_dispatcher.dispatch(msg);
}
}
}
catch (InterruptedException i) { }
catch (Exception e) { }
}
}
public void add (final Message m){
if (m == null)
return;
synchronized (_queue){
_queue.add(m);
_queue.notify();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当这段代码在我的 android 模拟器上运行时,我收到了很多如下警告:
Long monitor contention event with …Run Code Online (Sandbox Code Playgroud)