hangc0276 commented on code in PR #3838: URL: https://github.com/apache/bookkeeper/pull/3838#discussion_r1125774721
########## bookkeeper-common/src/main/java/org/apache/bookkeeper/common/collections/BatchedArrayBlockingQueue.java: ########## @@ -0,0 +1,409 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.bookkeeper.common.collections; + +import java.util.AbstractQueue; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * This implements a {@link BlockingQueue} backed by an array with fixed capacity. + * + * <p>This queue only allows 1 consumer thread to dequeue items and multiple producer threads. + */ +public class BatchedArrayBlockingQueue<T> + extends AbstractQueue<T> + implements BlockingQueue<T>, BatchedBlockingQueue<T> { + + private final ReentrantLock lock = new ReentrantLock(); + + private final Condition notEmpty = lock.newCondition(); + private final Condition notFull = lock.newCondition(); + + private final int capacity; + private final T[] data; + + private int size; + + private int consumerIdx; + private int producerIdx; + + @SuppressWarnings("unchecked") + public BatchedArrayBlockingQueue(int capacity) { + this.capacity = capacity; + this.data = (T[]) new Object[this.capacity]; + } + + private T dequeueOne() { + T item = data[consumerIdx]; + data[consumerIdx] = null; + if (++consumerIdx == capacity) { + consumerIdx = 0; + } + + if (size-- == capacity) { + notFull.signalAll(); + } + + return item; + } + + private void enqueueOne(T item) { + data[producerIdx] = item; + if (++producerIdx == capacity) { + producerIdx = 0; + } + + if (size++ == 0) { + notEmpty.signalAll(); + } + } + + @Override + public T poll() { + lock.lock(); + + try { + if (size == 0) { + return null; + } + + return dequeueOne(); + } finally { + lock.unlock(); + } + } + + @Override + public T peek() { + lock.lock(); + + try { + if (size == 0) { + return null; + } + + return data[consumerIdx]; + } finally { + lock.unlock(); + } + } + + @Override + public boolean offer(T e) { + lock.lock(); + + try { + if (size == capacity) { + return false; + } + + enqueueOne(e); + + return true; + } finally { + lock.unlock(); + } + } + + @Override + public void put(T e) throws InterruptedException { + lock.lockInterruptibly(); + + try { + while (size == capacity) { + notFull.await(); + } + + enqueueOne(e); + } finally { + lock.unlock(); + } + } + + public int putAll(List<T> c) throws InterruptedException { + lock.lockInterruptibly(); Review Comment: Do we need to check whether the items in the list is `null`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
