同步队列(SynchronizedQueue)
同步队列(SynchronizedQueue)存一个值必须先取出存入的值,才能继续存值,可以理解为容量为0的BlockingQueue。如果想存两个值,那就会一直等待阻塞。注意需要在线程中使用,普通方法使用时会发生阻塞
package com.luoKing.BlockingQueue; import java.util.concurrent.SynchronousQueue; public class sychronizedQueueDemo { public static void main(String[] args) throws InterruptedException { SynchronousQueue<String> queue = new SynchronousQueue<>(); new Thread( () -> { try { System.out.println(Thread.currentThread().getName()+" put a "); queue.put("a"); System.out.println(Thread.currentThread().getName()+" put b "); queue.put("b"); System.out.println(Thread.currentThread().getName()+" put c "); queue.put("c"); } catch (InterruptedException e) { e.printStackTrace(); } } ,"put").start(); new Thread(()->{ try { System.out.println(queue.take()); System.out.println(queue.take()); System.out.println(queue.take()); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } } //运行结果 put put a a put put b b put put c c
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决