一个队列,两个线程,一个线程负责往队列里add,另一个从队列removeFirst()
队列空时就wait(),add一个后就notify(),队列size超过100就丢弃add的对象
大多情况下执行没问题,但是总有一段时间只执行add,不执行removeFirst(),就算每次add后都执行notify()也不行
把队列size给添加到100,造成数据丢弃
请大家帮忙分析下为什么notify后不执行removeFirst(),大致的代码如下
public void SendMsg(byte[] msg) {
byte[] newmsg;
newmsg = msg.clone();
synchronized (m_out_q) {
// 不能过多。注意,过多时,不是等待,而是丢弃
if (m_out_q.size() >= m_maxsize)
return;
m_out_q.addLast(newmsg);
if (m_out_q.size() >= 1) {
m_out_q.notify();
}
}
} public void run() {
byte[] thismsg = null;
while (true) {
synchronized (m_out_q) {
while (m_out_q.isEmpty()) {
try {
m_out_q.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
thismsg = m_out_q.removeFirst();
} // end of synchronized

UDPService.sendMessage(thismsg);
}
}