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();
    }
}

おすすめ

転載: blog.csdn.net/fgets__/article/details/120781635