public ArrayBlockingQueue(int capacity) { this(capacity, false); } public ArrayBlockingQueue(int capacity, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); this.items = new Object[capacity];//创建指定大小的数组 lock = new ReentrantLock(fair);//创建锁 notEmpty = lock.newCondition(); notFull = lock.newCondition(); } //根据指定集合创建数组 public ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) { this(capacity, fair);
final ReentrantLock lock = this.lock; lock.lock(); // Lock only for visibility, not mutual exclusion//上锁 try { int i = 0; try { for (E e : c) { checkNotNull(e);//判空 items[i++] = e; } } catch (ArrayIndexOutOfBoundsException ex) { throw new IllegalArgumentException(); } count = i; putIndex = (i == capacity) ? 0 : i; } finally { lock.unlock(); } }