阻塞队列

182 阅读2分钟

数据结构与数据存储结构

数据结构:list,map,blockqueue, 是指相互之间存在一种或多种特定关系的数据元素的集合。 数据存储结构; 数组,链表, 描述数据在计算机中存储方式的学科 ,顺序存储(数组),非顺序存储(链表)

*队列、栈是线性数据结构的典型代表,而数组、链表是常用的两种数据存储结构;队列和栈均可以用数组或链表的存储方式实现它的功能!


BlockQueue --阻塞队列

Queue 这个接口 继承了Collection 与list 同级, BlockingQueue 接口 继承了Queue 接口;

1,ArrayBlockingQueue 有界 数组阻塞队列 -- ArrayList() 2,LinkedBlockingQueue有最大界的链表阻塞队列 --LinkedList(); 3

如下整理都是使用的api:

fe8f2acbd630c75437afc5dcc82c32eb.png

代码如下:

add(),remove(),element(), 超过界限值会抛异常


 public static void main(String[] args) {
        //需要给初始化值
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);
        //第一组方法, add, remove ,element 超出界限会抛异常, 比较刚
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("a"));
        //此处会抛异常
        blockingQueue.add("a");
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
        //此处会抛异常
        blockingQueue.remove();
        //获取头的第一个信息
        String element = blockingQueue.element();
       

offer(),pll(),peek(),超出界限会返回布尔值


public static void main(String[] args) {
        //需要给初始化值
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);
        //第二组方法, offer, poll ,peek 超出界限会返回布尔值;
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("a"));
        //此处会返回false
        System.out.println(blockingQueue.offer("a"));

        blockingQueue.poll();
        blockingQueue.poll();
        blockingQueue.poll();
        //此处会返回false
        blockingQueue.poll();
        //没有会返回null
        String peek = blockingQueue.peek();
    }

put(),take(), 超出界限会一直堵塞


public static void main(String[] args) throws InterruptedException {
        //需要给初始化值
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);
        //第三组方法, put take 超出界限会返回一直阻塞,比如消费消息;
        blockingQueue.put("a");
        blockingQueue.put("a");
        blockingQueue.put("a");
        //塞不进去会一直阻塞
        blockingQueue.put("a");
        
        blockingQueue.take();
        blockingQueue.take();
        blockingQueue.take();
        //取不到值会堵塞
        blockingQueue.take();
    }

offer(e,time,unit) poll(time,unit) 超出界限会返回等待一段时间,时间到还未完成则返回false;

 public static void main(String[] args) throws InterruptedException {
        //需要给初始化值
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);
        //第四组方法, offer(e,time,unit) poll(time,unit) 超出界限会返回等待一段时间;
        blockingQueue.offer("a", 2L, TimeUnit.SECONDS);
        blockingQueue.offer("a", 2L, TimeUnit.SECONDS);
        blockingQueue.offer("a", 2L, TimeUnit.SECONDS);
        //上面不会等待, 此处会等待2s,时间到了还未塞进去则返回false;
        blockingQueue.offer("a", 2L, TimeUnit.SECONDS);

        blockingQueue.poll(2L, TimeUnit.SECONDS);
        blockingQueue.poll(2L, TimeUnit.SECONDS);
        blockingQueue.poll(2L, TimeUnit.SECONDS);
        //上面不会等待, 此处会等待2s,时间到了还没东西删除则返回false;
        blockingQueue.poll(2L, TimeUnit.SECONDS);
    }

SynchronousQueue

阻塞队列 此队列没有容量,只有发现有消费(take)后才会去生产(put),切记

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class SyncQueueDemo {

    /**
     * SynchronousQueue 为没有容量的阻塞队列,看到有消费(take)才会生产(put);
     * @param args
     */
    public static void main(String[] args) throws Exception {
        BlockingQueue<String> syncBlockQueue = new SynchronousQueue<>();

        new Thread(() ->{
            try {
                syncBlockQueue.put("1");
                System.out.println(Thread.currentThread().getName() + " put 1");
                syncBlockQueue.put("2");
                System.out.println(Thread.currentThread().getName() + " put 2");
                syncBlockQueue.put("3");
                System.out.println(Thread.currentThread().getName() + " put 3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"AA").start();

        new Thread(() ->{
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+ " take " + syncBlockQueue.take());
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+ " take " + syncBlockQueue.take());
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+ " take " + syncBlockQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"BB").start();
    }

}