ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

Java ʵÏÖÉú²úÕß ¨C Ïû·ÑÕßÄ£ÐÍ(Èý)
2017-10-27 09:06:54 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:592´Î
Tags£ºJava ʵÏÖ Éú²úÕß Ïû·ÑÕß Ä£ÐÍ
.next; h.next = h; // help GC head = first; E x = first.item; first.item = null; return x; } ... /** * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity. * * @param capacity the capacity of this queue * @throws IllegalArgumentException if {@code capacity} is not greater * than zero */ public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node<E>(null); } ... /** * Inserts the specified element at the tail of this queue, waiting if * necessary for space to become available. * * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); // Note: convention in all put/take/etc is to preset local var // holding count negative to indicate failure unless set. int c = -1; Node<E> node = new Node<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(); } ... public E take() throws InterruptedException { E x; int c = -1; 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; } ... }

»¹´æÔڷdz£¶àµÄд·¨£¬ÈçÐźÅÁ¿Semaphore£¬Ò²ºÜ³£¼û£¨±¾¿Æ²Ù×÷ϵͳ½Ì²ÄÖеÄÉú²úÕß-Ïû·ÑÕßÄ£Ð;ÍÊÇÓÃÐźÅÁ¿ÊµÏֵģ©¡£²»¹ý×·¾¿¹ý¶àÁ˾ͺÃÏñÔÚ¾À½áÜîÏ㶹µÄд·¨Ò»Ñù£¬±¾ÎIJ»¼ÌÐø̽ÌÖ¡£

×ܽá

ʵÏÖÒ»±ØÐëÕÆÎÕ£¬ÊµÏÖËÄÖÁÉÙÒªÄÜÇå³þ±íÊö£»ÊµÏÖ¶þ¡¢ÈýÕÆÎÕÒ»¸ö¼´¿É¡£

Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 4 5 ÏÂÒ»Ò³ βҳ 3/5/5
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£º²¢·¢Ò»Ö¦»¨Ö® BlockingQueue ÏÂһƪ£ºArrayList ³õʼ»¯ ¨C Java ÄÇЩÊ..

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿