CircularBlockingQueue.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.tsi.app.common.xnet;
  2. import java.util.ArrayDeque;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.Objects;
  6. import java.util.concurrent.BlockingQueue;
  7. import java.util.concurrent.TimeUnit;
  8. import java.util.concurrent.locks.Condition;
  9. import java.util.concurrent.locks.ReentrantLock;
  10. public class CircularBlockingQueue<E> implements BlockingQueue<E> {
  11. private final ReentrantLock lock;
  12. private final Condition notEmpty;
  13. private final ArrayDeque<E> queue;
  14. private final int maxSize;
  15. public CircularBlockingQueue(int queueSize) {
  16. this.queue = new ArrayDeque<>(queueSize);
  17. this.maxSize = queueSize;
  18. this.lock = new ReentrantLock();
  19. this.notEmpty = this.lock.newCondition();
  20. }
  21. @Override
  22. public boolean offer(E e) {
  23. Objects.requireNonNull(e);
  24. final ReentrantLock lock = this.lock;
  25. lock.lock();
  26. try {
  27. if (this.queue.size() == this.maxSize) {
  28. return false;
  29. }
  30. this.queue.add(e);
  31. this.notEmpty.signal();
  32. } finally {
  33. lock.unlock();
  34. }
  35. return true;
  36. }
  37. public E offer_remove(E e) {
  38. Objects.requireNonNull(e);
  39. E discard = null;
  40. final ReentrantLock lock = this.lock;
  41. lock.lock();
  42. try {
  43. if (this.queue.size() == this.maxSize) {
  44. discard = this.queue.remove();
  45. }
  46. this.queue.add(e);
  47. this.notEmpty.signal();
  48. } finally {
  49. lock.unlock();
  50. }
  51. return discard;
  52. }
  53. @Override
  54. public E take() throws InterruptedException {
  55. final ReentrantLock lock = this.lock;
  56. lock.lockInterruptibly();
  57. try {
  58. while (this.queue.isEmpty()) {
  59. this.notEmpty.await();
  60. }
  61. return this.queue.poll();
  62. } finally {
  63. lock.unlock();
  64. }
  65. }
  66. @Override
  67. public boolean add(E e) {
  68. return false;
  69. }
  70. @Override
  71. public E remove() {
  72. return null;
  73. }
  74. @Override
  75. public E poll() {
  76. return null;
  77. }
  78. @Override
  79. public E element() {
  80. return null;
  81. }
  82. @Override
  83. public E peek() {
  84. return null;
  85. }
  86. @Override
  87. public void put(E e) throws InterruptedException {
  88. }
  89. @Override
  90. public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
  91. return false;
  92. }
  93. @Override
  94. public E poll(long timeout, TimeUnit unit) throws InterruptedException {
  95. return null;
  96. }
  97. @Override
  98. public int remainingCapacity() {
  99. return 0;
  100. }
  101. @Override
  102. public boolean remove(Object o) {
  103. return false;
  104. }
  105. @Override
  106. public boolean containsAll(Collection<?> c) {
  107. return false;
  108. }
  109. @Override
  110. public boolean addAll(Collection<? extends E> c) {
  111. return false;
  112. }
  113. @Override
  114. public boolean removeAll(Collection<?> c) {
  115. return false;
  116. }
  117. @Override
  118. public boolean retainAll(Collection<?> c) {
  119. return false;
  120. }
  121. @Override
  122. public void clear() {
  123. }
  124. @Override
  125. public int size() {
  126. return 0;
  127. }
  128. @Override
  129. public boolean isEmpty() {
  130. return false;
  131. }
  132. @Override
  133. public boolean contains(Object o) {
  134. return false;
  135. }
  136. @Override
  137. public Iterator<E> iterator() {
  138. return null;
  139. }
  140. @Override
  141. public Object[] toArray() {
  142. return new Object[0];
  143. }
  144. @Override
  145. public <T> T[] toArray(T[] a) {
  146. return null;
  147. }
  148. @Override
  149. public int drainTo(Collection<? super E> c) {
  150. return 0;
  151. }
  152. @Override
  153. public int drainTo(Collection<? super E> c, int maxElements) {
  154. return 0;
  155. }
  156. }