阻塞队列与同步队列

阻塞队列与同步队列

在这里插入图片描述

一、BlockingQueue

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

什么情况下会使用阻塞队列: 多线程并发处理,线程池!

学会使用队列

添加、移除

四组API

方式 抛出异常 有返回值,不抛出异常 阻塞等待 超时等待
添加 add offer() put offer(,)
移除 remove poll() take poll(,)
检测队首元素 element peek
public class testBlockingQueue {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        test4();
    }
    /**
     * 等待,阻塞(等待超时)
     */
    public static void test4() throws InterruptedException {
    
    
        // 队列的大小 
        ArrayBlockingQueue arrayBlockingQueue4 = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue4.offer("a"));
        System.out.println(arrayBlockingQueue4.offer("b"));
        System.out.println(arrayBlockingQueue4.offer("c"));
         // blockingQueue.offer("d",2,TimeUnit.SECONDS); // 等待超过2秒就退出 
        System.out.println(arrayBlockingQueue4.offer("d",2, TimeUnit.SECONDS));//超时等待,false,不会阻塞
        System.out.println("_____________");
        System.out.println(arrayBlockingQueue4.poll());
        System.out.println(arrayBlockingQueue4.poll());
        System.out.println(arrayBlockingQueue4.poll());
        System.out.println(arrayBlockingQueue4.poll(2,TimeUnit.SECONDS));//超时等待,null,不会抛出异常,等待超过2秒就退出

    }
    /**
     * 等待,阻塞(一直阻塞)
     */
    public static void test3() throws InterruptedException {
    
    
         // 队列的大小 
        ArrayBlockingQueue arrayBlockingQueue3 = new ArrayBlockingQueue<>(3);
       arrayBlockingQueue3.put("a");
       arrayBlockingQueue3.put("b");
       arrayBlockingQueue3.put("c");
//       arrayBlockingQueue3.put("d");// 队列没有位置了,一直阻塞
        System.out.println("_____________");
        System.out.println(arrayBlockingQueue3.take());
        System.out.println(arrayBlockingQueue3.take());
        System.out.println(arrayBlockingQueue3.take());
//        System.out.println(arrayBlockingQueue3.take());// 没有这个元素,一直阻塞
    }
    /**
     * 有返回值,不会抛出异常
     */
    public static void test2() {
    
    
        // 队列的大小 
        ArrayBlockingQueue arrayBlockingQueue2 = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue2.offer("a"));
        System.out.println(arrayBlockingQueue2.offer("b"));
        System.out.println(arrayBlockingQueue2.offer("c"));
        System.out.println(arrayBlockingQueue2.offer("d"));//false,不抛出异常!
        System.out.println("_____________");
        System.out.println(arrayBlockingQueue2.poll());
        System.out.println(arrayBlockingQueue2.poll());
        System.out.println(arrayBlockingQueue2.poll());
        System.out.println(arrayBlockingQueue2.poll());//null,不会抛出异常
    }
    /**
     * 抛出异常
     */
    public static void test() {
    
    

        // 队列的大小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
//        System.out.println(arrayBlockingQueue.add("d")); //IllegalStateException: Queue full
        System.out.println("_____________");
        System.out.println(arrayBlockingQueue.remove("b"));
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
//        System.out.println(arrayBlockingQueue.remove());//NoSuchElementException  抛出异常! 
    }
}

二、synchronousQueue 同步队列

没有容量,

进去一个元素,必须等待取出来之后,才能再往里面放一个元素

put、take

/**
 * 同步队列
 * 和其他的lockQueue 不一样, SynchronousQueue 不存储元素
 * put了一个元素,必须从里面先take取出来,否则不能在put进去值!
 */
public class SynchronousQueueDemo {
    
    

    public static void main(String[] args) {
    
    
        //同步队列
        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>(); 
        

        new Thread(()->{
    
    

            try {
    
    
                System.out.println(Thread.currentThread().getName() + "put 1");
                synchronousQueue.put("1");//没有被take,会阻塞
                System.out.println(Thread.currentThread().getName() + "put 2");
                synchronousQueue.put("2");
                System.out.println(Thread.currentThread().getName() + "put 3");
                synchronousQueue.put("3");
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        },"T1").start();

        new Thread(()->{
    
    
            try {
    
    
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "=>" + synchronousQueue.take()); //没有put,会阻塞
                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();
            } finally {
    
    
            }
        },"T2").start();
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43803285/article/details/115380876