123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package com.tsi.app.common.xnet;
- import java.util.ArrayDeque;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Objects;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.ReentrantLock;
- public class CircularBlockingQueue<E> implements BlockingQueue<E> {
- private final ReentrantLock lock;
- private final Condition notEmpty;
- private final ArrayDeque<E> queue;
- private final int maxSize;
- public CircularBlockingQueue(int queueSize) {
- this.queue = new ArrayDeque<>(queueSize);
- this.maxSize = queueSize;
- this.lock = new ReentrantLock();
- this.notEmpty = this.lock.newCondition();
- }
- @Override
- public boolean offer(E e) {
- Objects.requireNonNull(e);
- final ReentrantLock lock = this.lock;
- lock.lock();
- try {
- if (this.queue.size() == this.maxSize) {
- return false;
- }
- this.queue.add(e);
- this.notEmpty.signal();
- } finally {
- lock.unlock();
- }
- return true;
- }
- public E offer_remove(E e) {
- Objects.requireNonNull(e);
- E discard = null;
- final ReentrantLock lock = this.lock;
- lock.lock();
- try {
- if (this.queue.size() == this.maxSize) {
- discard = this.queue.remove();
- }
- this.queue.add(e);
- this.notEmpty.signal();
- } finally {
- lock.unlock();
- }
- return discard;
- }
- @Override
- public E take() throws InterruptedException {
- final ReentrantLock lock = this.lock;
- lock.lockInterruptibly();
- try {
- while (this.queue.isEmpty()) {
- this.notEmpty.await();
- }
- return this.queue.poll();
- } finally {
- lock.unlock();
- }
- }
- @Override
- public boolean add(E e) {
- return false;
- }
- @Override
- public E remove() {
- return null;
- }
- @Override
- public E poll() {
- return null;
- }
- @Override
- public E element() {
- return null;
- }
- @Override
- public E peek() {
- return null;
- }
- @Override
- public void put(E e) throws InterruptedException {
- }
- @Override
- public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
- return false;
- }
- @Override
- public E poll(long timeout, TimeUnit unit) throws InterruptedException {
- return null;
- }
- @Override
- public int remainingCapacity() {
- return 0;
- }
- @Override
- public boolean remove(Object o) {
- return false;
- }
- @Override
- public boolean containsAll(Collection<?> c) {
- return false;
- }
- @Override
- public boolean addAll(Collection<? extends E> c) {
- return false;
- }
- @Override
- public boolean removeAll(Collection<?> c) {
- return false;
- }
- @Override
- public boolean retainAll(Collection<?> c) {
- return false;
- }
- @Override
- public void clear() {
- }
- @Override
- public int size() {
- return 0;
- }
- @Override
- public boolean isEmpty() {
- return false;
- }
- @Override
- public boolean contains(Object o) {
- return false;
- }
- @Override
- public Iterator<E> iterator() {
- return null;
- }
- @Override
- public Object[] toArray() {
- return new Object[0];
- }
- @Override
- public <T> T[] toArray(T[] a) {
- return null;
- }
- @Override
- public int drainTo(Collection<? super E> c) {
- return 0;
- }
- @Override
- public int drainTo(Collection<? super E> c, int maxElements) {
- return 0;
- }
- }
|