JUC并发编程学习笔记-10-阻塞队列BlockingQueue

视频链接
JUC并发编程学习笔记-10-阻塞队列BlockingQueue_第1张图片
JUC并发编程学习笔记-10-阻塞队列BlockingQueue_第2张图片

1.抛出异常

import java.util.concurrent.ArrayBlockingQueue;

public class bqTest {
    public static void main(String[] args) {
        //list
        //set
        test1();
    }
    /*
    抛出异常
     */
    public static void test1(){
        //队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        //java.lang.IllegalStateException抛出异常
        //System.out.println(blockingQueue.add("d"));
        System.out.println(blockingQueue.element());//查看队首元素是谁
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //java.util.NoSuchElementException没有元素了
        //System.out.println(blockingQueue.remove());


    }
}

2.有返回值,没有异常


import java.util.concurrent.ArrayBlockingQueue;

public class bqTest {
    public static void main(String[] args) {
        //list
        //set
        test2();
    }
/*
有返回值没有异常
 */
    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("++++++++++++++++++++++++++++++++++++");
        System.out.println(blockingQueue.peek());//查看队首元素是谁
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());//null  不抛出异常
    }
}

3.等待阻塞,一直


import java.util.concurrent.ArrayBlockingQueue;

public class bqTest {
    public static void main(String[] args) throws InterruptedException {
        //list
        //set
        test3();
    }
/*
等待,阻塞(一直)

 */
    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());//没有这个元素,一直等待,阻塞
    }
    /*
等待,阻塞(等待超时)

 */
}

4.等待,阻塞(等待超时)


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class bqTest {
    public static void main(String[] args) throws InterruptedException {
        //list
        //set
        test3();
    }
    /*
等待,阻塞(等待超时)

 */
    public static void test3() throws InterruptedException {
        //队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        blockingQueue.offer("d",2, TimeUnit.SECONDS);//等待超过两秒就退出

        System.out.println("===========");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        blockingQueue.poll(2,TimeUnit.SECONDS);//超过两秒就退出
    }

}

你可能感兴趣的:(JUC并发编程)