ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ArrayBlockingQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ArrayBlockingQueue.java (file contents):
Revision 1.57 by jsr166, Tue Sep 1 22:18:53 2009 UTC vs.
Revision 1.58 by jsr166, Mon Jul 12 20:15:19 2010 UTC

# Line 55 | Line 55 | public class ArrayBlockingQueue<E> exten
55      private static final long serialVersionUID = -817911632652898426L;
56  
57      /** The queued items  */
58 <    private final E[] items;
58 >    final E[] items;
59      /** items index for next take, poll or remove */
60 <    private int takeIndex;
60 >    int takeIndex;
61      /** items index for next put, offer, or add. */
62 <    private int putIndex;
62 >    int putIndex;
63      /** Number of items in the queue */
64      private int count;
65  
# Line 69 | Line 69 | public class ArrayBlockingQueue<E> exten
69       */
70  
71      /** Main lock guarding all access */
72 <    private final ReentrantLock lock;
72 >    final ReentrantLock lock;
73      /** Condition for waiting takes */
74      private final Condition notEmpty;
75      /** Condition for waiting puts */
76      private final Condition notFull;
77  
78 +    /** Predicate for the notEmpty condition */
79 +    boolean empty() { return count == 0; }
80 +    /** Predicate for the notFull condition */
81 +    boolean full() { return count == items.length; }
82 +
83      // Internal helper methods
84  
85      /**
86       * Circularly increment i.
87       */
88      final int inc(int i) {
89 <        return (++i == items.length)? 0 : i;
89 >        return (++i == items.length) ? 0 : i;
90      }
91  
92      /**
# Line 222 | Line 227 | public class ArrayBlockingQueue<E> exten
227          final ReentrantLock lock = this.lock;
228          lock.lock();
229          try {
230 <            if (count == items.length)
230 >            if (full())
231                  return false;
232              else {
233                  insert(e);
# Line 242 | Line 247 | public class ArrayBlockingQueue<E> exten
247       */
248      public void put(E e) throws InterruptedException {
249          if (e == null) throw new NullPointerException();
245        final E[] items = this.items;
250          final ReentrantLock lock = this.lock;
251          lock.lockInterruptibly();
252          try {
253 <            try {
254 <                while (count == items.length)
251 <                    notFull.await();
252 <            } catch (InterruptedException ie) {
253 <                notFull.signal(); // propagate to non-interrupted thread
254 <                throw ie;
255 <            }
253 >            while (full())
254 >                notFull.await();
255              insert(e);
256          } finally {
257              lock.unlock();
# Line 275 | Line 274 | public class ArrayBlockingQueue<E> exten
274          final ReentrantLock lock = this.lock;
275          lock.lockInterruptibly();
276          try {
277 <            for (;;) {
279 <                if (count != items.length) {
280 <                    insert(e);
281 <                    return true;
282 <                }
277 >            while (full()) {
278                  if (nanos <= 0)
279                      return false;
280 <                try {
286 <                    nanos = notFull.awaitNanos(nanos);
287 <                } catch (InterruptedException ie) {
288 <                    notFull.signal(); // propagate to non-interrupted thread
289 <                    throw ie;
290 <                }
280 >                nanos = notFull.awaitNanos(nanos);
281              }
282 +            insert(e);
283 +            return true;
284          } finally {
285              lock.unlock();
286          }
# Line 298 | Line 290 | public class ArrayBlockingQueue<E> exten
290          final ReentrantLock lock = this.lock;
291          lock.lock();
292          try {
293 <            if (count == 0)
302 <                return null;
303 <            E x = extract();
304 <            return x;
293 >            return empty() ? null : extract();
294          } finally {
295              lock.unlock();
296          }
# Line 311 | Line 300 | public class ArrayBlockingQueue<E> exten
300          final ReentrantLock lock = this.lock;
301          lock.lockInterruptibly();
302          try {
303 <            try {
304 <                while (count == 0)
305 <                    notEmpty.await();
317 <            } catch (InterruptedException ie) {
318 <                notEmpty.signal(); // propagate to non-interrupted thread
319 <                throw ie;
320 <            }
321 <            E x = extract();
322 <            return x;
303 >            while (empty())
304 >                notEmpty.await();
305 >            return extract();
306          } finally {
307              lock.unlock();
308          }
# Line 330 | Line 313 | public class ArrayBlockingQueue<E> exten
313          final ReentrantLock lock = this.lock;
314          lock.lockInterruptibly();
315          try {
316 <            for (;;) {
334 <                if (count != 0) {
335 <                    E x = extract();
336 <                    return x;
337 <                }
316 >            while (empty()) {
317                  if (nanos <= 0)
318                      return null;
319 <                try {
341 <                    nanos = notEmpty.awaitNanos(nanos);
342 <                } catch (InterruptedException ie) {
343 <                    notEmpty.signal(); // propagate to non-interrupted thread
344 <                    throw ie;
345 <                }
346 <
319 >                nanos = notEmpty.awaitNanos(nanos);
320              }
321 +            return extract();
322          } finally {
323              lock.unlock();
324          }
# Line 354 | Line 328 | public class ArrayBlockingQueue<E> exten
328          final ReentrantLock lock = this.lock;
329          lock.lock();
330          try {
331 <            return (count == 0) ? null : items[takeIndex];
331 >            return empty() ? null : items[takeIndex];
332          } finally {
333              lock.unlock();
334          }
# Line 643 | Line 617 | public class ArrayBlockingQueue<E> exten
617              int i = takeIndex;
618              int n = 0;
619              int sz = count;
620 <            int max = (maxElements < count)? maxElements : count;
620 >            int max = (maxElements < count) ? maxElements : count;
621              while (n < max) {
622                  c.add(items[i]);
623                  items[i] = null;
# Line 708 | Line 682 | public class ArrayBlockingQueue<E> exten
682  
683          Itr() {
684              lastRet = -1;
685 <            if (count == 0)
685 >            if (empty())
686                  nextIndex = -1;
687              else {
688                  nextIndex = takeIndex;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines