Rat*_*tha 5 java blockingqueue
我正在我的程序中使用阻塞队列实现.我想知道线程将等待一个元素出队多长时间.我的客户通过民意调查回复,我的服务器线程提供了消息.我的代码如下;
private BlockingQueue<Message> applicationResponses= new LinkedBlockingQueue<Message>();
Run Code Online (Sandbox Code Playgroud)
客户:
Message response = applicationResponses.take();
Run Code Online (Sandbox Code Playgroud)
服务器:
applicationResponses.offer(message);
Run Code Online (Sandbox Code Playgroud)
我的客户线程会永远等待吗?我想配置那个时间..(例如:1000ms)..这可能吗?
小智 9
是的,它将永远等待,直到你可以采取一个元素.如果您想拥有最长等待时间,则应使用poll(time,TimeUnit).
Message response = applicationResponse.poll(1, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
请参阅:http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html#poll(long,%20java.util.concurrent.TimeUnit)
| 归档时间: |
|
| 查看次数: |
6825 次 |
| 最近记录: |