294 |
|
*/ |
295 |
|
public void put(E e) throws InterruptedException { |
296 |
|
if (e == null) throw new NullPointerException(); |
297 |
< |
// Note: convention in all put/take/etc is to preset local var |
298 |
< |
// holding count negative to indicate failure unless set. |
299 |
< |
int c = -1; |
300 |
< |
Node<E> node = new Node<E>(e); |
297 |
> |
final int c; |
298 |
> |
final Node<E> node = new Node<E>(e); |
299 |
|
final ReentrantLock putLock = this.putLock; |
300 |
|
final AtomicInteger count = this.count; |
301 |
|
putLock.lockInterruptibly(); |
336 |
|
|
337 |
|
if (e == null) throw new NullPointerException(); |
338 |
|
long nanos = unit.toNanos(timeout); |
339 |
< |
int c = -1; |
339 |
> |
final int c; |
340 |
|
final ReentrantLock putLock = this.putLock; |
341 |
|
final AtomicInteger count = this.count; |
342 |
|
putLock.lockInterruptibly(); |
374 |
|
final AtomicInteger count = this.count; |
375 |
|
if (count.get() == capacity) |
376 |
|
return false; |
377 |
< |
int c = -1; |
378 |
< |
Node<E> node = new Node<E>(e); |
377 |
> |
final int c; |
378 |
> |
final Node<E> node = new Node<E>(e); |
379 |
|
final ReentrantLock putLock = this.putLock; |
380 |
|
putLock.lock(); |
381 |
|
try { |
382 |
< |
if (count.get() < capacity) { |
383 |
< |
enqueue(node); |
384 |
< |
c = count.getAndIncrement(); |
385 |
< |
if (c + 1 < capacity) |
386 |
< |
notFull.signal(); |
387 |
< |
} |
382 |
> |
if (count.get() == capacity) |
383 |
> |
return false; |
384 |
> |
enqueue(node); |
385 |
> |
c = count.getAndIncrement(); |
386 |
> |
if (c + 1 < capacity) |
387 |
> |
notFull.signal(); |
388 |
|
} finally { |
389 |
|
putLock.unlock(); |
390 |
|
} |
391 |
|
if (c == 0) |
392 |
|
signalNotEmpty(); |
393 |
< |
return c >= 0; |
393 |
> |
return true; |
394 |
|
} |
395 |
|
|
396 |
|
public E take() throws InterruptedException { |
397 |
< |
E x; |
398 |
< |
int c = -1; |
397 |
> |
final E x; |
398 |
> |
final int c; |
399 |
|
final AtomicInteger count = this.count; |
400 |
|
final ReentrantLock takeLock = this.takeLock; |
401 |
|
takeLock.lockInterruptibly(); |
416 |
|
} |
417 |
|
|
418 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
419 |
< |
E x = null; |
420 |
< |
int c = -1; |
419 |
> |
final E x; |
420 |
> |
final int c; |
421 |
|
long nanos = unit.toNanos(timeout); |
422 |
|
final AtomicInteger count = this.count; |
423 |
|
final ReentrantLock takeLock = this.takeLock; |
444 |
|
final AtomicInteger count = this.count; |
445 |
|
if (count.get() == 0) |
446 |
|
return null; |
447 |
< |
E x = null; |
448 |
< |
int c = -1; |
447 |
> |
final E x; |
448 |
> |
final int c; |
449 |
|
final ReentrantLock takeLock = this.takeLock; |
450 |
|
takeLock.lock(); |
451 |
|
try { |
452 |
< |
if (count.get() > 0) { |
453 |
< |
x = dequeue(); |
454 |
< |
c = count.getAndDecrement(); |
455 |
< |
if (c > 1) |
456 |
< |
notEmpty.signal(); |
457 |
< |
} |
452 |
> |
if (count.get() == 0) |
453 |
> |
return null; |
454 |
> |
x = dequeue(); |
455 |
> |
c = count.getAndDecrement(); |
456 |
> |
if (c > 1) |
457 |
> |
notEmpty.signal(); |
458 |
|
} finally { |
459 |
|
takeLock.unlock(); |
460 |
|
} |
464 |
|
} |
465 |
|
|
466 |
|
public E peek() { |
467 |
+ |
final AtomicInteger count = this.count; |
468 |
|
if (count.get() == 0) |
469 |
|
return null; |
470 |
|
final ReentrantLock takeLock = this.takeLock; |