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

Comparing jsr166/src/main/java/util/concurrent/LinkedBlockingDeque.java (file contents):
Revision 1.22 by jsr166, Wed Jul 29 20:53:30 2009 UTC vs.
Revision 1.23 by dl, Tue Sep 28 11:05:19 2010 UTC

# Line 97 | Line 97 | public class LinkedBlockingDeque<E>
97           */
98          Node<E> next;
99  
100 <        Node(E x, Node<E> p, Node<E> n) {
100 >        Node(E x) {
101              item = x;
102            prev = p;
103            next = n;
102          }
103      }
104  
# Line 170 | Line 168 | public class LinkedBlockingDeque<E>
168              for (E e : c) {
169                  if (e == null)
170                      throw new NullPointerException();
171 <                if (!linkLast(e))
171 >                if (!linkLast(new Node<E>(e)))
172                      throw new IllegalStateException("Deque full");
173              }
174          } finally {
# Line 182 | Line 180 | public class LinkedBlockingDeque<E>
180      // Basic linking and unlinking operations, called only while holding lock
181  
182      /**
183 <     * Links e as first element, or returns false if full.
183 >     * Links node as first element, or returns false if full.
184       */
185 <    private boolean linkFirst(E e) {
185 >    private boolean linkFirst(Node<E> node) {
186          // assert lock.isHeldByCurrentThread();
187          if (count >= capacity)
188              return false;
189          Node<E> f = first;
190 <        Node<E> x = new Node<E>(e, null, f);
191 <        first = x;
190 >        node.next = f;
191 >        first = node;
192          if (last == null)
193 <            last = x;
193 >            last = node;
194          else
195 <            f.prev = x;
195 >            f.prev = node;
196          ++count;
197          notEmpty.signal();
198          return true;
199      }
200  
201      /**
202 <     * Links e as last element, or returns false if full.
202 >     * Links node as last element, or returns false if full.
203       */
204 <    private boolean linkLast(E e) {
204 >    private boolean linkLast(Node<E> node) {
205          // assert lock.isHeldByCurrentThread();
206          if (count >= capacity)
207              return false;
208          Node<E> l = last;
209 <        Node<E> x = new Node<E>(e, l, null);
210 <        last = x;
209 >        node.prev = l;
210 >        last = node;
211          if (first == null)
212 <            first = x;
212 >            first = node;
213          else
214 <            l.next = x;
214 >            l.next = node;
215          ++count;
216          notEmpty.signal();
217          return true;
# Line 310 | Line 308 | public class LinkedBlockingDeque<E>
308       */
309      public boolean offerFirst(E e) {
310          if (e == null) throw new NullPointerException();
311 +        Node<E> node = new Node<E>(e);
312          final ReentrantLock lock = this.lock;
313          lock.lock();
314          try {
315 <            return linkFirst(e);
315 >            return linkFirst(node);
316          } finally {
317              lock.unlock();
318          }
# Line 324 | Line 323 | public class LinkedBlockingDeque<E>
323       */
324      public boolean offerLast(E e) {
325          if (e == null) throw new NullPointerException();
326 +        Node<E> node = new Node<E>(e);
327          final ReentrantLock lock = this.lock;
328          lock.lock();
329          try {
330 <            return linkLast(e);
330 >            return linkLast(node);
331          } finally {
332              lock.unlock();
333          }
# Line 339 | Line 339 | public class LinkedBlockingDeque<E>
339       */
340      public void putFirst(E e) throws InterruptedException {
341          if (e == null) throw new NullPointerException();
342 +        Node<E> node = new Node<E>(e);
343          final ReentrantLock lock = this.lock;
344          lock.lock();
345          try {
346 <            while (!linkFirst(e))
346 >            while (!linkFirst(node))
347                  notFull.await();
348          } finally {
349              lock.unlock();
# Line 355 | Line 356 | public class LinkedBlockingDeque<E>
356       */
357      public void putLast(E e) throws InterruptedException {
358          if (e == null) throw new NullPointerException();
359 +        Node<E> node = new Node<E>(e);
360          final ReentrantLock lock = this.lock;
361          lock.lock();
362          try {
363 <            while (!linkLast(e))
363 >            while (!linkLast(node))
364                  notFull.await();
365          } finally {
366              lock.unlock();
# Line 372 | Line 374 | public class LinkedBlockingDeque<E>
374      public boolean offerFirst(E e, long timeout, TimeUnit unit)
375          throws InterruptedException {
376          if (e == null) throw new NullPointerException();
377 +        Node<E> node = new Node<E>(e);
378          long nanos = unit.toNanos(timeout);
379          final ReentrantLock lock = this.lock;
380          lock.lockInterruptibly();
381          try {
382 <            while (!linkFirst(e)) {
382 >            while (!linkFirst(node)) {
383                  if (nanos <= 0)
384                      return false;
385                  nanos = notFull.awaitNanos(nanos);
# Line 394 | Line 397 | public class LinkedBlockingDeque<E>
397      public boolean offerLast(E e, long timeout, TimeUnit unit)
398          throws InterruptedException {
399          if (e == null) throw new NullPointerException();
400 +        Node<E> node = new Node<E>(e);
401          long nanos = unit.toNanos(timeout);
402          final ReentrantLock lock = this.lock;
403          lock.lockInterruptibly();
404          try {
405 <            while (!linkLast(e)) {
405 >            while (!linkLast(node)) {
406                  if (nanos <= 0)
407                      return false;
408                  nanos = notFull.awaitNanos(nanos);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines