阻塞队列出现原因
1、很多场景要求分离生产者、消费者两个角色,题目得由不同的线程来担当,而之前的实现根本没有考虑多线程的情况
2、队列为空,那么在之前的实现里面会返回null,如果就是硬要拿到一个元素呢?原来的方式只能不断循环尝试寻找
3、队列为满,那么再之前的实现里面会返回false,如果就是硬要塞入一个元素呢?原来的方式只能不断循环尝试寻找
解决方法:
1、用锁保证线程安全
2、用条件变量让poll或offer线程进入等待状态,而不是不断循环尝试,让CPU空转
核心问题:
多线程导致添加队列时线程错乱,可能会覆盖值或者和预期的不对
单锁实现阻塞队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| public class BlockingQueue1<E> implements BlockingQueue<E> { private final E[] array; private int head; private int tail; private int size;
@SuppressWarnings("all") public BlockingQueue1(int capacity) { array = (E[]) new Object[capacity]; }
private ReentrantLock lock = new ReentrantLock(); private Condition headWaits = lock.newCondition(); private Condition tailWaits = lock.newCondition();
private boolean isEmpty() { return size == 0; }
private boolean isFull() { return size == array.length; }
@Override public void offer(E e) throws InterruptedException { lock.lockInterruptibly(); try{ while(isFull()) { tailWaits.await(); } array[tail] = e; if(++tail == array.length) { tail = 0; } size++; headWaits.signal(); } finally { lock.unlock(); }
}
@Override public boolean offer(E e, long timeout) throws InterruptedException { lock.lockInterruptibly(); try{ long nanos = TimeUnit.MILLISECONDS.toNanos(timeout); while(isFull()) { if(nanos <= 0) { return false; } nanos = tailWaits.awaitNanos(nanos); } array[tail] = e; if(++tail == array.length) { tail = 0; } size++; headWaits.signal(); } finally { lock.unlock(); } return true; }
@Override public E poll() throws InterruptedException { lock.lockInterruptibly(); try { while(isEmpty()){ headWaits.await(); } E e = array[head]; array[head] = null; if(++head == array.length) { head = 0; } size--; tailWaits.signal(); return e; } finally { lock.unlock(); } } }
|
双锁实现阻塞队列(完整版)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| public class BlockingQueue2<E> implements BlockingQueue<E> {
private final E[] array; private int head; private int tail; private AtomicInteger size;
@SuppressWarnings("all") public BlockingQueue2(int capacity) { array = (E[]) new Object[capacity]; }
private ReentrantLock tailLock = new ReentrantLock(); private Condition tailWaits = tailLock.newCondition();
private ReentrantLock headLock = new ReentrantLock(); private Condition headWaits = tailLock.newCondition();
private boolean isEmpty() { return size.get() == 0; }
private boolean isFull() { return size.get() == array.length; }
@Override public void offer(E e) throws InterruptedException { tailLock.lockInterruptibly(); int c; try { while(isFull()) { tailWaits.await(); }
array[tail] = e; if(++tail == array.length) { tail = 0; } c = size.getAndIncrement(); if(c + 1 < array.length) { tailWaits.signal(); }
} finally { tailLock.unlock(); } if(c == 0) { headLock.lock(); try { headWaits.signal(); } finally { headLock.unlock(); } }
}
@Override public boolean offer(E e, long timeout) throws InterruptedException { return true; }
@Override public E poll() throws InterruptedException { E e; int c; headLock.lockInterruptibly(); try { while(isEmpty()) { headWaits.await(); } e = array[head]; array[head] = null; if(++head == array.length) { head = 0; } c = size.getAndDecrement();
if(c > 1) { headWaits.signal(); }
}finally { headLock.unlock(); } if(c == array.length) { tailLock.lock(); try { tailWaits.signal(); } finally { tailLock.unlock(); } } return e; } }
|