//LinkedBlockingQueue的put()方法publicvoidput(E e)throws InterruptedException {if(e == null)thrownewNullPointerException();finalint c;final Node<E> node =newNode<E>(e);final ReentrantLock putLock =this.putLock;final AtomicInteger count =this.count;//获取锁
putLock.lockInterruptibly();try{/*
* Note that count is used in wait guard even though it is
* not protected by lock. This works because count can
* only decrease at this point (all other puts are shut
* out by lock), and we (or some other waiting put) are
* signalled if it ever changes from capacity. Similarly
* for all other uses of count in other wait guards.
*/while(count.get()== capacity){
notFull.await();}//元素入队enqueue(node);
c = count.getAndIncrement();if(c +1< capacity)
notFull.signal();}finally{//释放锁
putLock.unlock();}if(c ==0)signalNotEmpty();}//LinkedBlockingQueue的take()方法public E take()throws InterruptedException {final E x;finalint c;final AtomicInteger count =this.count;final ReentrantLock takeLock =this.takeLock;//获取锁
takeLock.lockInterruptibly();try{while(count.get()==0){
notEmpty.await();}
x =dequeue();
c = count.getAndDecrement();if(c >1)
notEmpty.signal();}finally{//释放锁
takeLock.unlock();}if(c == capacity)signalNotFull();return x;}
PriorityBlockingQueue
//PriorityBlockingQueue的put()方法publicvoidput(E e){//锁的操作在offer()方法中offer(e);// never need to block}//PriorityBlockingQueue的offer()方法publicbooleanoffer(E e){if(e == null)thrownewNullPointerException();final ReentrantLock lock =this.lock;//获取锁
lock.lock();int n, cap;
Object[] es;while((n = size)>=(cap =(es = queue).length))tryGrow(es, cap);try{final Comparator<?super E> cmp;if((cmp = comparator)== null)siftUpComparable(n, e, es);elsesiftUpUsingComparator(n, e, es, cmp);
size = n +1;
notEmpty.signal();}finally{//释放锁
lock.unlock();}returntrue;}//PriorityBlockingQueue的take()方法public E take()throws InterruptedException {final ReentrantLock lock =this.lock;//获取锁
lock.lockInterruptibly();
E result;try{while((result =dequeue())== null)
notEmpty.await();}finally{//释放锁
lock.unlock();}return result;}