JUC编程—阻塞队列学习

JUC并发编程—阻塞队列学习

在这里插入图片描述

在这里插入图片描述

1、四组API

方式抛出异常不抛出异常,有返回值阻塞等待超时等待
添加add()offer()put()offer(,)
移除remove()poll()take()poll(,)
检测队首元素element()peek()~~
 	/**
     * 抛出异常
     */
 	public static void test1(){
        //队列的大小为3
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);


         // 添加
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("b"));
		System.out.println(blockingQueue.element());//弹出队首元素

        // java.lang.IllegalStateException: Queue full 抛出异常 队列已满
       // System.out.println(blockingQueue.add("D"));
        System.out.println("====================");
        //取出
        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("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));

        //System.out.println(blockingQueue.offer("d"));//返回值为false
        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());//返回值为:null
    }
 /**
     * 阻塞等待(一直等待)
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("A");
        blockingQueue.put("B");
        blockingQueue.put("C");
        //blockingQueue.put("D");//队列没有位置,一直阻塞
        
        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);
        blockingQueue.offer("A");
        blockingQueue.offer("B");
        blockingQueue.offer("C");
        //blockingQueue.offer("D",2, TimeUnit.SECONDS); //等待2秒钟如果没有位置,就退出
        
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //blockingQueue.poll(2,TimeUnit.SECONDS);//等待2秒,如果没有就退出
    }

2、SynchronizedQueue(同步队列)

package com.zkw.JUC并发编程.bq;

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

/**
 * 同步队列
 * 和其它的BlockingQueue 不一样, SynchronizedQueue 不存储元素
 * put了一个元素,必须从里面先take取出来,否则就不能put进去值!
 */
public class SynchronizedQueueTest {
    public static void main(String[] args) {
        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"=>"+"Put A");
                synchronousQueue.put("A");
                System.out.println(Thread.currentThread().getName()+"=>"+"Put B");
                synchronousQueue.put("B");
                System.out.println(Thread.currentThread().getName()+"=>"+"Put C");
                synchronousQueue.put("C");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T1").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+"-->"+synchronousQueue.take());
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+"-->"+synchronousQueue.take());
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+"-->"+synchronousQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T2").start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值