假设我们有一个队列
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));
https://stackoverflow.com/questions/23462209
复制相似问题