BlockingQueue

- Collection->Queue->BlockingQueue
- 使用场景:多线程并发处理,线程池
- Queue源码
| public interface Queue<E> extends Collection<E> { |
| boolean add(E e); |
| boolean offer(E e); |
| E remove(); |
| E poll(); |
| E element(); |
| E peek(); |
| } |
| public interface BlockingQueue<E> extends Queue<E> { |
| boolean add(E e); |
| boolean offer(E e); |
| void put(E e) throws InterruptedException; |
| boolean offer(E e, long timeout, TimeUnit unit) |
| throws InterruptedException; |
| E take() throws InterruptedException; |
| E poll(long timeout, TimeUnit unit) |
| throws InterruptedException; |
| int remainingCapacity(); |
| boolean remove(Object o); |
| boolean contains(Object o); |
| int drainTo(Collection<? super E> c); |
| int drainTo(Collection<? super E> c, int maxElements); |
| } |
ArrayBlockingQueue
方式 |
抛出异常 |
不抛出异常 |
阻塞等待 |
超时等待 |
添加 |
add(o) |
offer(o) |
put(o) |
offer(o, timeout, timeunit) |
移除 |
remove() |
poll() |
take() |
poll(timeout, timeunit) |
判断队首 |
element() |
peek() |
--- |
--- |
| import java.util.concurrent.ArrayBlockingQueue; |
| import java.util.concurrent.TimeUnit; |
| |
| class MyBlockingQueue { |
| public static void main(String[] args) throws InterruptedException { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| test4(); |
| } |
| |
| public static void test1() { |
| |
| ArrayBlockingQueue<Object> 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.element()); |
| |
| System.out.println(arrayBlockingQueue.remove()); |
| System.out.println(arrayBlockingQueue.remove()); |
| System.out.println(arrayBlockingQueue.remove()); |
| |
| } |
| |
| public static void test2() { |
| |
| ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3); |
| System.out.println(arrayBlockingQueue.offer("a")); |
| System.out.println(arrayBlockingQueue.offer("b")); |
| System.out.println(arrayBlockingQueue.offer("c")); |
| System.out.println(arrayBlockingQueue.offer("d")); |
| |
| System.out.println(arrayBlockingQueue.peek()); |
| |
| System.out.println(arrayBlockingQueue.poll()); |
| System.out.println(arrayBlockingQueue.poll()); |
| System.out.println(arrayBlockingQueue.poll()); |
| System.out.println(arrayBlockingQueue.poll()); |
| |
| } |
| |
| public static void test3() throws InterruptedException { |
| |
| ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3); |
| |
| |
| arrayBlockingQueue.put("a"); |
| arrayBlockingQueue.put("b"); |
| arrayBlockingQueue.put("c"); |
| |
| |
| System.out.println(arrayBlockingQueue.take()); |
| System.out.println(arrayBlockingQueue.take()); |
| System.out.println(arrayBlockingQueue.take()); |
| |
| } |
| |
| public static void test4() throws InterruptedException { |
| |
| ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3); |
| arrayBlockingQueue.offer("a"); |
| arrayBlockingQueue.offer("b"); |
| arrayBlockingQueue.offer("c"); |
| arrayBlockingQueue.offer("d", 2, TimeUnit.SECONDS); |
| |
| System.out.println(arrayBlockingQueue.poll()); |
| System.out.println(arrayBlockingQueue.poll()); |
| System.out.println(arrayBlockingQueue.poll()); |
| System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS)); |
| System.out.println("over"); |
| } |
| } |
SynchronousQueue同步队列
-
使用CAS实现线程的安全访问
-
构造函数,默认非公平
| public SynchronousQueue() { |
| this(false); |
| } |
| |
| public SynchronousQueue(boolean fair) { |
| |
| |
| transferer = fair ? new TransferQueue<E>() : new TransferStack<E>(); |
| } |
- 它一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然。同步队列没有任何内部容量,甚至连一个队列的容量都没有。
| import java.util.concurrent.BlockingQueue; |
| import java.util.concurrent.SynchronousQueue; |
| import java.util.concurrent.TimeUnit; |
| |
| class MySynchronousQueue { |
| public static void main(String[] args) { |
| |
| BlockingQueue<String> blockingQueue = new SynchronousQueue<>(); |
| |
| new Thread(() -> { |
| try { |
| System.out.println(Thread.currentThread().getName() + " put\t1"); |
| |
| blockingQueue.put("1"); |
| System.out.println(Thread.currentThread().getName() + " put\t2"); |
| blockingQueue.put("2"); |
| System.out.println(Thread.currentThread().getName() + " put\t3"); |
| blockingQueue.put("3"); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| }, "Producer").start(); |
| |
| new Thread(() -> { |
| try { |
| TimeUnit.SECONDS.sleep(1); |
| |
| System.out.println(Thread.currentThread().getName() + " take\t" +blockingQueue.take()); |
| TimeUnit.SECONDS.sleep(1); |
| System.out.println(Thread.currentThread().getName() + " take\t" +blockingQueue.take()); |
| TimeUnit.SECONDS.sleep(1); |
| System.out.println(Thread.currentThread().getName() + " take\t" +blockingQueue.take()); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| }, "Consumer").start(); |
| } |
| } |
LinkedBlockingQueue
| |
| public LinkedBlockingQueue() { |
| this(Integer.MAX_VALUE); |
| } |
| |
| public LinkedBlockingQueue(int capacity) { |
| if (capacity <= 0) throw new IllegalArgumentException(); |
| this.capacity = capacity; |
| last = head = new Node<E>(null); |
| } |
PriorityBlockingQueue
- 带优先级的无界阻塞队列
- 每次出队都返回优先级最高的元素,是二叉树最小堆的实现
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/15486652.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步