7.阻塞队列

阻塞队列

四组API
方式 抛出异常 有返回值 阻塞 等待 超时等待
添加 add offer put offer(元素,等待时间,等待时间单位)
移除 remove poll take poll(元素,等待时间,等待时间单位)
过去队列首 element peek
public class Test {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /*
     * 1.抛出异常
     * */
    public static void test1() {
        //队列大小
        BlockingQueue queue = new ArrayBlockingQueue(3);

        //添加
        System.out.println(queue.add("a"));//true
        System.out.println(queue.add("b"));//true
        System.out.println(queue.add("c"));//true
        //System.out.println(queue.add("d"));//IllegalStateException Queue full 队列已满
        System.out.println(queue.element());//a 首元素

        //移除
        System.out.println(queue.remove());//a
        System.out.println(queue.remove());//b
        System.out.println(queue.remove());//c
        System.out.println(queue.remove());//NoSuchElementException 没有元素了
    }

    /*
     * 2.抛出异常
     * */
    public static void test2() {
        //队列大小
        BlockingQueue queue = new ArrayBlockingQueue(3);

        //添加
        System.out.println(queue.offer("a"));//true
        System.out.println(queue.offer("b"));//true
        System.out.println(queue.offer("c"));//true
        System.out.println(queue.offer("d"));//false
        System.out.println(queue.peek());
        //移除
        System.out.println(queue.poll());//a
        System.out.println(queue.poll());//b
        System.out.println(queue.poll());//c
        System.out.println(queue.poll());
    }

    /*
    * 3.阻塞,等待
    * */
    public static void test3() throws InterruptedException {
        BlockingQueue queue = new ArrayBlockingQueue<>(3);

        //添加
        queue.put("a");
        queue.put("b");
        queue.put("c");
        //queue.put("d");//队列没有位置了,一直阻塞,等待

        //移除
        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());//一直阻塞,等待
    }

    /*
    * 4.等待 超时
    * */
    public static void test4() throws InterruptedException {
        BlockingQueue queue=new ArrayBlockingQueue(3);
        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("b"));
        System.out.println(queue.offer("c"));
        System.out.println(queue.offer("d",2, TimeUnit.SECONDS));//等待2秒后退出,false
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll(2,TimeUnit.SECONDS));//等待2秒后,null

    }
}

SynchronousQueue

/*
* 同步队列
* 和其他BlockingQueue不一样,SynchronousQueue不能存贮元素,
* put一个元素,必须先take一个元素
* */
public class SynchronousQueueTest {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new SynchronousQueue<>();
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + " put a");
                queue.put("a");
                System.out.println("queue:"+queue);

                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();
            }
        }, "t1").start();


        /*new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + " take " + queue.take());

                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + " take " + queue.take());

                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + " take " + queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2").start();*/

    }
}
posted @   jpy  阅读(1)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示