Unity Animator Transition参数详解:告别动画抽搐,打造流畅角色动画
2026/7/14 21:43:42
阻塞队列是一种特殊的队列,其遵循“先入先出”的原则。
阻塞队列也是一种线程安全的数据结构,具有以下特性:
“生产者消费者模型”是阻塞队列的一个典型应用场景,该模型也是一个典型的开发模型。
生产者消费者模型就是通过一个中间容器来解决生产者和消费者之间的强耦合问题。
这个中间容器通过阻塞队列实现,从而使生产者和消费者之间不进行直接通讯。
阻塞队列的作用:
阻塞队列的缺点:
- 引入队列以后,代码整体结构变复杂
- 程序执行效率有所影响
Java的标准库中,提供了现成的阻塞队列。
public class demo1 { public static void main(String[] args) { //创建阻塞队列 BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000); //生产者线程 Thread producer = new Thread(() -> { int n = 0; while (true) { try { queue.put(n); System.out.println("生产元素 " + n); n++; } catch (InterruptedException e) { throw new RuntimeException(e); } } }, "procducer"); //消费者线程 Thread consumer = new Thread(() -> { while(true){ try { int n = queue.take(); System.out.println("消费元素 " + n); } catch (InterruptedException e) { throw new RuntimeException(e); } } }, "consumer"); producer.start(); consumer.start(); } }class BlockingQueue { private int[] item = new int[1000]; private volatile int head = 0; private volatile int tail = 0; private volatile int size = 0; //生产元素 public void put(int value) throws InterruptedException { synchronized (this) { while (size == item.length) { this.wait(); } item[tail] = value; tail++; if (tail == item.length) { tail = 0; } size++; notifyAll(); } } //消费元素 public int take() throws InterruptedException { synchronized (this) { while (size == 0) { this.wait(); } int ret = item[head]; head++; if(head == item.length){ head = 0; } size--; notifyAll(); return ret; } } //获取内部属性 public synchronized int getSize() { return size; } }