我在我的程序中使用阻塞队列.. 我的客户端threds轮询响应,并且服务器thred提供对queue的响应。我没有使用来自客户端的任何thread.sleep机制,因为即时通讯使用了blockqueue ..但是当我的服务器线程延迟到时,我从队列中取出空值把一些消息放入队列.. 为什么会这样? 我的代码;
private BlockingQueue<Message> applicationResponses= new LinkedBlockingQueue<Message>();
client
--------
Message response = applicationResponses.poll();
server;
applicationResponses.offer(message);
答案 0 :(得分:3)
使用take
代替poll
- take
将阻塞线程直到对象准备好,而poll
将在队列为空时返回null