流 API 和队列:订阅阻塞队列流样式
2022-09-01 11:28:55
假设我们有一个队列
BlockingQueue<String> queue= new LinkedBlockingQueue<>();
和其他一些线程在其中放置值,然后我们像这样阅读它
while (true) {
String next = queue.take();
System.out.println("next message:" + next);
}
我如何以流样式迭代此队列,同时保持与上述代码相似的语义。
此代码仅遍历当前队列状态:
queue.stream().forEach(e -> System.out.println(e));