简体   繁体   English

阻塞队列不阻塞线程?

[英]Blocking queue is not blocking the thread?

im using blocking queue in my program.. where my client threds poll for response, and server thred offer the response to queue.I didnt use any thread.sleep mechanism from client end, since im using blockingqueue..But im egtting null values from the queue, when my server thread delays to put some messages in the queue.. Why this is happening? 我在程序中使用阻塞队列..我的客户端请求轮询响应,服务器请求响应给队列。我没有使用客户端的任何thread.sleep机制,因为我使用blockingqueue ..但是我从队列,当我的服务器线程延迟将一些消息放入队列时。为什么会发生这种情况? My code; 我的代码;

private BlockingQueue<Message> applicationResponses=  new LinkedBlockingQueue<Message>();

client
--------
    Message response = applicationResponses.poll();

server;
    applicationResponses.offer(message);

使用take代替poll - take将阻塞线程,直到对象准备就绪,而如果队列为空,则poll将仅返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM