JUC-java.util.concurrent学习(三)---BlockingQueue

该博客是JUC-java.util.concurrent学习的第三部分,主要介绍了BlockingQueue。Queue与List、Set同级,其下有阻塞队列、双端队列、非阻塞队列等。阻塞队列在队列为空取元素或队列满放元素时会阻塞,还提到了四组API及SynchronousQueue。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JUC-java.util.concurrent学习(三)

BlockingQueue

Queue和List,Set是同一级的,Queue下面有阻塞队列 BlockingQueue,双端队列BlockingDeque,非阻塞队列AbstractQueue

阻塞:在队列为空拿去的时候会阻塞,在队列满了往里面放的时候会阻塞

四组API

操作抛出异常有返回值,不抛出异常阻塞等待超时等待
添加addofferputoffer
移除removepolltakepoll
判断首elementpeek
/**
 * 抛出异常
 */
public static void test1(){
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
    //返回true或者false
    System.out.println(blockingQueue.add("1"));
    System.out.println(blockingQueue.add("2"));
    System.out.println(blockingQueue.add("3"));
    //java.lang.IllegalStateException: Queue full
    //System.out.println(blockingQueue.add("3"));

    //判断首位
    System.out.println(blockingQueue.element());
    
    System.out.println("-----------");

    //qu去除的对象
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());
    //java.util.NoSuchElementException
    //System.out.println(blockingQueue.remove());
}
/**
 * 有返回值,不抛出异常
 */
public static void test2(){
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    System.out.println(blockingQueue.offer("1"));
    System.out.println(blockingQueue.offer("2"));
    System.out.println(blockingQueue.offer("3"));
    //false,不抛出异常
    //System.out.println(blockingQueue.offer("4"));

    //判断首位
    System.out.println(blockingQueue.peek());

    System.out.println("-----------");

    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    //返回null
    //System.out.println(blockingQueue.poll());
}
/**
 * 阻塞等待
 */
public static void test3() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    blockingQueue.put("1");
    blockingQueue.put("2");
    blockingQueue.put("3");
    //blockingQueue.put("1");//一直等待



    //判断首位
    System.out.println(blockingQueue.peek());

    System.out.println("-----------");

    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    System.out.println(blockingQueue.take());
    //System.out.println(blockingQueue.take());//一直等待
}
/**
 * 超时等待
 */
public static void test4() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

    System.out.println(blockingQueue.offer("1"));
    System.out.println(blockingQueue.offer("2"));
    System.out.println(blockingQueue.offer("3"));
    //超时等待
    System.out.println(blockingQueue.offer("4",2, TimeUnit.SECONDS));

    //判断首位
    System.out.println(blockingQueue.peek());

    System.out.println("-----------");

    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    System.out.println(blockingQueue.poll());
    //超时等待
    System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
}

SynchronousQueue

/**
 * SynchronousQueue同步队列,不存储元素
 * 放入一个值就必须要取出一个值
 */
public class synchronizedQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<String>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"放入了第一个");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName()+"放入了第二个");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName()+"放入了第三个");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T1").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"->"+blockingQueue.take());
                System.out.println(Thread.currentThread().getName()+"->"+blockingQueue.take());
                System.out.println(Thread.currentThread().getName()+"->"+blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T2").start();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值