前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【JUC】005-阻塞队列BlockingQueue、同步队列SynchronousQueue

【JUC】005-阻塞队列BlockingQueue、同步队列SynchronousQueue

作者头像
訾博ZiBo
发布2025-01-06 16:42:40
发布2025-01-06 16:42:40
7600
代码可运行
举报
运行总次数:0
代码可运行

一、阻塞队列BlockingQueue

1、说明

(不得不阻塞)

写入:如果队列满了,就必须阻塞等待;

读取:如果队列是空的,就必须阻塞等待;

2、阻塞队列

3、结构图

4、BlockingQueue的4组API

有返回值抛出异常代码演示:
代码语言:javascript
代码运行次数:0
复制
    //有返回值抛出异常代码演示
    @Test
    public void test01(){
        //队列的大小
        ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(3);

        System.out.println(queue.add("a"));
        System.out.println(queue.add("b"));
        System.out.println(queue.add("c"));
        //此时队列满了,再添加则会抛出异常:java.lang.IllegalStateException: Queue full
//        System.out.println(queue.add("d"));

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

        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());
        //此时队列空了,再移除则会抛出异常:java.util.NoSuchElementException
//        System.out.println(queue.remove());
    }
有返回值不抛出异常代码演示:
代码语言:javascript
代码运行次数:0
复制
    //有返回值不抛出异常代码演示
    @Test
    public void test02(){
        //队列的大小
        ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(3);

        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("b"));
        System.out.println(queue.offer("c"));
        //此时队列满了,返回false,不抛出异常
        System.out.println(queue.offer("d"));

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

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        //此时队列空了,返回null,不抛出异常
        System.out.println(queue.poll());
    }
阻塞等待:
代码语言:javascript
代码运行次数:0
复制
    //阻塞等待
    @Test
    public void test03() throws InterruptedException {
        //队列的大小
        ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(3);

        queue.put("a");
        queue.put("b");
        queue.put("c");
        //此时队列满了,阻塞等待
//        queue.put("d");

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

        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        //此时队列空了,阻塞等待
//        System.out.println(queue.take());
    }
超时等待:
代码语言:javascript
代码运行次数:0
复制
    //超时等待
    @Test
    public void test04() throws InterruptedException {
        //队列的大小
        ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(3);

        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        //此时队列满了,超时等待
        queue.offer("d",2, TimeUnit.SECONDS);//等待超过两秒就退出

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

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        //此时队列空了,超时等待
        System.out.println(queue.poll(2,TimeUnit.SECONDS));//等待超过两秒就退出

    }

二、同步队列SynchronousQueue

1、概述

存放一个元素,必须等待取出之后才能存放下一个元素;

2、代码演示

代码实现:
代码语言:javascript
代码运行次数:0
复制
package com.zibo.sq;

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

//同步队列
public class TestSynchronousQueue {
    public static void main(String[] args) {
        SynchronousQueue<Object> queue = new SynchronousQueue<>();
        //存线程
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName() + "put1");
                queue.put("1");
                System.out.println(Thread.currentThread().getName() + "put2");
                queue.put("2");
                System.out.println(Thread.currentThread().getName() + "put3");
                queue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"put线程").start();
        //取线程
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + queue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + queue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"take线程").start();
    }
}
运行结果:

存1-取1-存2-取2-存3-取3

代码语言:javascript
代码运行次数:0
复制
put线程put1
take线程1
put线程put2
take线程2
put线程put3
take线程3
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、阻塞队列BlockingQueue
    • 1、说明
    • 2、阻塞队列
    • 3、结构图
    • 4、BlockingQueue的4组API
      • 有返回值抛出异常代码演示:
      • 有返回值不抛出异常代码演示:
      • 阻塞等待:
      • 超时等待:
  • 二、同步队列SynchronousQueue
    • 1、概述
    • 2、代码演示
      • 代码实现:
      • 运行结果:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档