JUC高并发(JUC)之BlockingQueue 阻塞队列

这次讲解的是BlockingQueue
BlockingQueue(阻塞队列)是什么?
当队列为空时,从队列中获取元素将阻塞
当队列为满时,从队列中添加元素将阻塞
因为是队列,所以我们理应想到先进先出
接下来我们来演示简单的使用一下:

add(抛出异常):

/**
 * @author Cocowwy
 * @create 2020-05-05-14:53
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {
    BlockingQueue<String> 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.add("x"));
    }
}

结果如下:

true
true
true
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 juc.BlockingQueueDemo.main(BlockingQueueDemo.java:16)

我们可以看到如果添加的元素大于3之后,即当队列满了之后会抛异常:java.lang.IllegalStateException: Queue full

remove(抛出异常):

public class BlockingQueueDemo {
    public static void main(String[] args) {
    BlockingQueue<String> 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.add("x"));
        System.out.println(blockingQueue.remove()); //返回值是E(即对象) 因为是队列 所以是a
        System.out.println(blockingQueue.remove()); 
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
    }
}

这里需要注意的是remove的返回类型是E(对象)
结果如下:

true
true
true
a
b
c
Exception in thread "main" java.util.NoSuchElementException
	at java.util.AbstractQueue.remove(AbstractQueue.java:117)
	at juc.BlockingQueueDemo.main(BlockingQueueDemo.java:20)

Process finished with exit code 1

我们可以看到如果添加的元素大于3之后,即当队列空了之后再删会抛异常:java.util.NoSuchElementException

element(抛出异常):

public class BlockingQueueDemo {
    public static void main(String[] args) {
    BlockingQueue<String> 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.element());
    }
}

结果如下:

true
true
true
a

检查队首元素是什么

offer(特殊值):

public class BlockingQueueDemo {
    public static void main(String[] args) {
    BlockingQueue<String> 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"));
    }
}

结果如下:

true
true
true
false

插入,返回成功

pull(特殊值):

public class BlockingQueueDemo {
    public static void main(String[] args) {
    BlockingQueue<String> 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.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }
}

结果如下:

true
true
true
a
b
c
null

peek(特殊值):

public class BlockingQueueDemo {
    public static void main(String[] args) {
    BlockingQueue<String> 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.peek());
    }
}

结果如下:

true
true
true
a

put(阻塞):

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
    BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
    blockingQueue.put("a");
    blockingQueue.put("b");
    blockingQueue.put("c");
    blockingQueue.put("d");
    }
}

结果如下:


就是啥都没有一片空白,因为此时一直阻塞,等待,与接下来的take进行对比

take(阻塞):

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        System.out.println(blockingQueue.take());

        blockingQueue.put("e");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }
}

结果如下:

a
b
c
e

与上面的put对比一下,这里abc添加进去后如果想加e是加不进去的,会阻塞,而take进行取出后就能加入e了

offer(超时):

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> 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", 3L, TimeUnit.SECONDS));
    }
}

结果如下:

true
true
true
false  //等待了3s

让其等待3s,返回值为boolean

poll(超时):

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> 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.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
    }
}

结果如下:

true
true
true
a
b
c
null //等待了3s

综合上述情况我们可以得到此图:
JUC高并发(JUC)之BlockingQueue 阻塞队列_第1张图片

你可能感兴趣的:(高并发,javase)