Java併發程式設計——阻塞佇列

YaDe.發表於2020-12-20

阻塞佇列

一、阻塞佇列(BlockingQueue )

image-20200718234207836
四種操作方式

方式丟擲異常不丟擲異常,有返回值阻塞等待超時等待
新增add()offer()put()offer(E e,long timeout, TimeUnit unit)過載方法
移除remove()poll()take()poll(long timeout, TimeUnit unit)過載方法
檢測隊首元素element()peek()
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }
    //丟擲異常
    public static void test1(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
    }
    //有返回值
    public static void test2(){
        BlockingQueue 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"));
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }
    //阻塞等待
    public static void test3() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        blockingQueue.put("d"); //會一直等
    }
    //超時等待
    public static void test4() throws InterruptedException {
        BlockingQueue 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",2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
    }
}

執行結果

1、丟擲異常
Exception in thread "main" java.lang.IllegalStateException: Queue full
	at java.util.AbstractQueue.add(AbstractQueue.java:98)
	at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312)
	at gdut.hzh.blockingqueue.BlockingQueueDemo.test1(BlockingQueueDemo.java:21)
	at gdut.hzh.blockingqueue.BlockingQueueDemo.main(BlockingQueueDemo.java:13)
Exception in thread "main" java.util.NoSuchElementException
    at java.util.AbstractQueue.remove(AbstractQueue.java:117)
	at gdut.hzh.blockingqueue.BlockingQueueDemo.test1(BlockingQueueDemo.java:24)
	at gdut.hzh.blockingqueue.BlockingQueueDemo.main(BlockingQueueDemo.java:13)
2、有返回值
true
true
true
false
a
b
c
null
3、阻塞等待
4、超時等待
true
true
true
壓入第四個,超時等待2s
false
a
b
c
彈出第四個,超時等待2s
null

二、同步佇列(SynchronousQueue)

沒有容量,進去一個元素,必須等待取出來之後,才能往裡面放一個元素
放進去:put,取出來:take
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+" put 1");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName()+" put 2");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName()+" put 3");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T1").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"==>"+blockingQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"==>"+blockingQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+"==>"+blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T2").start();
    }
}

執行結果:

T1 put 1
T2==>1
T1 put 2
T2==>2
T1 put 3
T2==>3

相關文章