定义

一个阻塞式的队列,使用数据的数据结构继承自AbstractQueue

  1. //返回 boolean 实际使用的是add
  2. public boolean add(E e) {
  3. if (this.offer(e)) {
  4. return true;
  5. } else {
  6. throw new IllegalStateException("Queue full");
  7. }
  8. }
  1. //抛出异常
  2. public boolean offer(E e) {
  3. Objects.requireNonNull(e);
  4. ReentrantLock lock = this.lock;
  5. lock.lock();
  6. boolean var3;
  7. try {
  8. if (this.count != this.items.length) {
  9. this.enqueue(e);
  10. var3 = true;
  11. return var3;
  12. }
  13. var3 = false;
  14. } finally {
  15. lock.unlock();
  16. }
  17. return var3;
  18. }
  1. // 阻塞
  2. public void put(E e) throws InterruptedException {
  3. Objects.requireNonNull(e);
  4. ReentrantLock lock = this.lock;
  5. lock.lockInterruptibly();
  6. try {
  7. while(this.count == this.items.length) {
  8. this.notFull.await();
  9. }
  10. this.enqueue(e);
  11. } finally {
  12. lock.unlock();
  13. }
  14. }