/hazelcast/src/main/java/com/hazelcast/util/SimpleBlockingQueue.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 162 lines · 123 code · 23 blank · 16 comment · 21 complexity · 7a487d7e41ac9dc5284735a7f969f29d MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.util;
  17. import java.util.AbstractQueue;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.LinkedList;
  21. import java.util.concurrent.BlockingQueue;
  22. import java.util.concurrent.TimeUnit;
  23. public class SimpleBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E> {
  24. private final Object lock = new Object();
  25. private final LinkedList<E> items = new LinkedList<E>();
  26. private final LinkedList<E> prioritizedItems;
  27. public SimpleBlockingQueue() {
  28. this(false);
  29. }
  30. public SimpleBlockingQueue(boolean priorityAware) {
  31. prioritizedItems = (priorityAware) ? new LinkedList<E>() : null;
  32. }
  33. public boolean offer(E e) {
  34. put(e);
  35. return true;
  36. }
  37. public void put(E e) {
  38. synchronized (lock) {
  39. if (prioritizedItems != null && e instanceof Prioritized) {
  40. prioritizedItems.add(e);
  41. } else {
  42. items.add(e);
  43. }
  44. //noinspection CallToNotifyInsteadOfNotifyAll
  45. lock.notify();
  46. }
  47. }
  48. public boolean remove(Object obj) {
  49. synchronized (lock) {
  50. boolean removed = items.remove(obj);
  51. if (!removed && prioritizedItems != null) {
  52. removed = prioritizedItems.remove(obj);
  53. }
  54. return removed;
  55. }
  56. }
  57. public E take() throws InterruptedException {
  58. return poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  59. }
  60. public E poll() {
  61. synchronized (lock) {
  62. return removeFirst();
  63. }
  64. }
  65. private E removeFirst() {
  66. E e = null;
  67. if (prioritizedItems != null && prioritizedItems.size() > 0) {
  68. e = prioritizedItems.removeFirst();
  69. } else if (items.size() > 0) {
  70. e = items.removeFirst();
  71. }
  72. return e;
  73. }
  74. private int totalSize() {
  75. return items.size() + ((prioritizedItems == null) ? 0 : prioritizedItems.size());
  76. }
  77. @SuppressWarnings("CallToNativeMethodWhileLocked")
  78. public E poll(long timeout, TimeUnit unit) throws InterruptedException {
  79. long timeLeft = unit.toMillis(timeout);
  80. long start = Clock.currentTimeMillis();
  81. synchronized (lock) {
  82. E e = removeFirst();
  83. while (e == null && timeLeft > 0) {
  84. lock.wait(timeLeft);
  85. e = removeFirst();
  86. long now = Clock.currentTimeMillis();
  87. timeLeft -= (now - start);
  88. start = now;
  89. }
  90. return e;
  91. }
  92. }
  93. public void clear() {
  94. synchronized (lock) {
  95. items.clear();
  96. if (prioritizedItems != null) {
  97. prioritizedItems.clear();
  98. }
  99. }
  100. }
  101. public boolean add(E e) {
  102. put(e);
  103. return true;
  104. }
  105. public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
  106. put(e);
  107. return true;
  108. }
  109. public int remainingCapacity() {
  110. return Integer.MAX_VALUE;
  111. }
  112. public int drainTo(Collection<? super E> c) {
  113. return drainTo(c, Integer.MAX_VALUE);
  114. }
  115. public int drainTo(Collection<? super E> c, int maxElements) {
  116. synchronized (lock) {
  117. int count = 0;
  118. E removed = removeFirst();
  119. while (removed != null && count > maxElements) {
  120. c.add(removed);
  121. removed = removeFirst();
  122. }
  123. return count;
  124. }
  125. }
  126. public E peek() {
  127. throw new UnsupportedOperationException();
  128. }
  129. @Override
  130. public Iterator<E> iterator() {
  131. throw new UnsupportedOperationException();
  132. }
  133. @Override
  134. public int size() {
  135. synchronized (lock) {
  136. return totalSize();
  137. }
  138. }
  139. }