{"id":682,"date":"2013-12-25T17:33:53","date_gmt":"2013-12-25T09:33:53","guid":{"rendered":"http:\/\/coderbee.net\/?p=682"},"modified":"2013-12-25T17:33:53","modified_gmt":"2013-12-25T09:33:53","slug":"juc-arrayblockingqueue","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131225\/682","title":{"rendered":"JUC  ArrayBlockingQueue"},"content":{"rendered":"<p><code>java.util.concurrent.ArrayBlockingQueue<\/code> \u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u3001\u57fa\u4e8e\u6570\u7ec4\u3001\u6709\u754c\u7684\u3001\u963b\u585e\u7684\u3001FIFO \u961f\u5217\u3002\u8bd5\u56fe\u5411\u5df2\u6ee1\u961f\u5217\u4e2d\u653e\u5165\u5143\u7d20\u4f1a\u5bfc\u81f4\u64cd\u4f5c\u53d7\u963b\u585e\uff1b\u8bd5\u56fe\u4ece\u7a7a\u961f\u5217\u4e2d\u63d0\u53d6\u5143\u7d20\u5c06\u5bfc\u81f4\u7c7b\u4f3c\u963b\u585e\u3002<\/p>\n<p>\u6b64\u7c7b\u57fa\u4e8e <code>java.util.concurrent.locks.ReentrantLock<\/code> \u6765\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\uff0c\u6240\u4ee5\u63d0\u4f9b\u4e86 <code>ReentrantLock<\/code> \u6240\u80fd\u652f\u6301\u7684\u516c\u5e73\u6027\u9009\u62e9\u3002<\/p>\n<h4>\u5c5e\u6027<\/h4>\n<p>\u961f\u5217\u7684\u64cd\u4f5c\u4e3b\u8981\u6709\u8bfb\u3001\u5199\uff0c\u6240\u4ee5\u7528\u4e86\u4e24\u4e2a <code>int<\/code> \u7c7b\u578b\u7684\u5c5e\u6027\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u8bfb\u5199\u4f4d\u7f6e\u7684\u7684\u6307\u9488\u3002\u5b58\u653e\u5143\u7d20\u7684\u6570\u7ec4\u662f <code>final<\/code> \u4fee\u9970\u7684\uff0c\u6240\u4ee5\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\u3002\u5bf9\u4e8e\u5e76\u53d1\u63a7\u5236\uff0c\u662f\u6240\u6709\u7684\u8bbf\u95ee\u90fd\u5fc5\u987b\u52a0\u9501\uff0c\u5e76\u7528\u4e24\u4e2a\u6761\u4ef6\u5bf9\u8c61\u7528\u4e8e\u534f\u8c03\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u961f\u5217\u5b58\u653e\u5143\u7d20\u7684\u5bb9\u5668\nfinal Object[] items;\n\n\/\/ \u4e0b\u4e00\u6b21\u8bfb\u53d6\u6216\u79fb\u9664\u7684\u4f4d\u7f6e\nint takeIndex;\n\n\/\/ \u5b58\u653e\u4e0b\u4e00\u4e2a\u653e\u5165\u5143\u7d20\u7684\u4f4d\u7f6e\nint putIndex;\n\n\/\/ \u961f\u5217\u91cc\u6709\u6548\u5143\u7d20\u7684\u6570\u91cf\nint count;\n\n\n\/\/ \u6240\u6709\u8bbf\u95ee\u7684\u4fdd\u62a4\u9501\nfinal ReentrantLock lock;\n\n\/\/ \u7b49\u5f85\u83b7\u53d6\u7684\u6761\u4ef6\nprivate final Condition notEmpty;\n\n\/\/ \u7b49\u5f85\u653e\u5165\u7684\u6761\u4ef6\nprivate final Condition notFull;\n<\/code><\/pre>\n<h4>\u73af\u7ed5\u5904\u7406<\/h4>\n<p>\u5982\u679c\u6307\u9488\u4e00\u76f4\u5f80\u524d\u589e\u52a0\u6216\u4e00\u76f4\u5f80\u540e\u51cf\u5c0f\uff0c\u90a3\u4e48\u603b\u4f1a\u8d85\u51fa\u6570\u7ec4\u7684\u6709\u6548\u7d22\u5f15\u8303\u56f4\u3002\u6240\u4ee5\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u73af\u7ed5\u5904\u7406\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u6307\u9488\u524d\u79fb\nfinal int inc(int i) {\n    return (++i == items.length) ? 0 : i;\n}\n\n\/\/ \u6307\u9488\u540e\u79fb\nfinal int dec(int i) {\n    return ((i == 0) ? items.length : i) - 1;\n}\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u5904\u7406\u90fd\u662f\u5bf9\u6307\u9488\u503c\u7684\u76f4\u63a5\u5904\u7406\uff0c\u800c\u4e0d\u5173\u5fc3\u662f\u8bfb\u6307\u9488\u8fd8\u662f\u5199\u6307\u9488\uff0c\u56e0\u4e3a\u662f\u5426\u6709\u53ef\u8bfb\u5143\u7d20\u3001\u53ef\u5199\u7a7a\u95f4\u7684\u5224\u65ad\u662f\u901a\u8fc7\u5bf9 <code>count<\/code> \u8ba1\u6570\u6765\u5224\u65ad\u7684\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f <code>count<\/code> \u7684\u4f5c\u7528\uff0c\u5b83\u6781\u5927\u5730\u7b80\u5316\u4e86\u6307\u9488\u6709\u6548\u6027\u7684\u5224\u65ad\u3002\u5728\u4e0b\u9762\u7684 <code>insert<\/code> \u548c <code>extract<\/code> \u65b9\u6cd5\u4e2d\u6839\u672c\u5c31\u4e0d\u9700\u8981\u5bf9\u8bfb\u5199\u6307\u9488\u4e4b\u95f4\u7684\u4f4d\u7f6e\u5173\u7cfb\u8fdb\u884c\u5224\u65ad\uff0c\u975e\u5e38\u7cbe\u5999\u3002<\/p>\n<p>\u901a\u8fc7\u73af\u7ed5\u5904\u7406\u53ef\u4ee5\u628a\u8fd9\u4e2a\u6570\u7ec4\u770b\u6210\u662f\u5706\u5f62\u7684\u7f13\u5b58\u3002<br \/>\n<!--more--><\/p>\n<h4>\u6dfb\u52a0\u5143\u7d20<\/h4>\n<p>\u6240\u6709\u6dfb\u52a0\u64cd\u4f5c\u6700\u7ec8\u90fd\u662f\u8c03\u7528\u5230\u5185\u90e8\u65b9\u6cd5 <code>insert<\/code>\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u5728\u6301\u6709\u9501\u7684\u524d\u63d0\u4e0b\u8c03\u7528\nprivate void insert(E x) {\n    items[putIndex] = x;\n    putIndex = inc(putIndex); \/\/ \u6307\u9488\u524d\u79fb 1\n    ++count; \/\/ \u6709\u6548\u5143\u7d20\u6570\u91cf\u52a0 1\n    notEmpty.signal(); \/\/ \u901a\u77e5\u5728\u975e\u7a7a\u6761\u4ef6\u4e0a\u7b49\u5f85\u7684\u8bfb\u7ebf\u7a0b\n}\n<\/code><\/pre>\n<h4>\u8bfb\u53d6\u5143\u7d20<\/h4>\n<p>\u6240\u6709\u8bfb\u53d6\u64cd\u4f5c\u6700\u7ec8\u90fd\u662f\u8c03\u7528\u5230\u5185\u90e8\u65b9\u6cd5 <code>extract<\/code>\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u5728\u6301\u6709\u9501\u7684\u524d\u63d0\u4e0b\u8c03\u7528\nprivate E extract() {\n    final Object[] items = this.items;\n    E x = this.&lt;E&gt;cast(items[takeIndex]);\n    items[takeIndex] = null; \/\/ for GC\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u9732\uff1b\u4e5f\u7528\u4e8e\u5224\u65ad\u5143\u7d20\u662f\u5426\u88ab\u79fb\u9664\n    takeIndex = inc(takeIndex); \/\/ \u6307\u9488\u524d\u79fb 1\n    --count; \/\/ \u6709\u6548\u5143\u7d20\u6570\u91cf\u51cf 1\n    notFull.signal(); \/\/ \u901a\u77e5\u5728\u975e\u6ee1\u6761\u4ef6\u4e0a\u7b49\u5f85\u7684\u5199\u7ebf\u7a0b\n    return x;\n}\n<\/code><\/pre>\n<h4>\u79fb\u9664\u6307\u5b9a\u4f4d\u7f6e\u5143\u7d20<\/h4>\n<pre><code class=\"java\">\/\/ \u5728\u6301\u6709\u9501\u7684\u524d\u63d0\u4e0b\u8c03\u7528\nvoid removeAt(int i) {\n    final Object[] items = this.items;\n    \/\/ \u5982\u679c\u8981\u79fb\u9664\u662f\u5143\u7d20\u5c31\u662f\u4e0b\u4e00\u4e2a\u53ef\u8bfb\u6570\u636e\uff0c\u76f4\u63a5\u79fb\u9664\u3001\u4fee\u6539\u8bfb\u6307\u9488\u5373\u53ef\u3002\n    \/\/ \u8fd9\u662f\u4e00\u79cd\u4f18\u5316\uff0c\u907f\u514d\u6570\u636e\u62f7\u8d1d\u3002\n    if (i == takeIndex) {\n        items[takeIndex] = null;\n        takeIndex = inc(takeIndex);\n    } else {\n      \/\/ \u5982\u679c\u8981\u79fb\u9664\u5143\u7d20\u662f\u5728\u6709\u6548\u6570\u636e\u7684\u4e2d\u95f4\uff0c\u90a3\u4e48\u8981\u628a\u5b83\u4e4b\u540e\u6dfb\u52a0\u7684\u5143\u7d20\u540e\u79fb\n      \/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc\u4e0d\u80fd\u7528\u8bfb\u5199\u6307\u9488\u7684\u5927\u5c0f\u5173\u7cfb\u4f5c\u4e3a\u7ec8\u7ed3\u6761\u4ef6\uff0c\u4e5f\u662f\u56e0\u4e3a\u73af\u7ed5\u3002\n        for (;;) {\n            int nexti = inc(i);\n            if (nexti != putIndex) {\n                items[i] = items[nexti];\n                i = nexti;\n            } else {\n                items[i] = null; \/\/ for GC\n                putIndex = i; \/\/ putIndex \u4e0d\u662f\u76f4\u63a5\u51cf 1 \u8fd8\u662f\u56e0\u4e3a\u73af\u7ed5\u3002\n                break;\n            }\n        }\n    }\n    --count;\n    notFull.signal(); \/\/\n}\n<\/code><\/pre>\n<h4>\u65b9\u6cd5\u52a0\u9501<\/h4>\n<p>\u4f5c\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u7c7b\uff0c<code>ArrayBlockingQueue<\/code> \u7684\u6240\u6709\u516c\u5f00\u65b9\u6cd5\u7684\u903b\u8f91\u90fd\u662f\u5728\u52a0\u9501\u7684\u524d\u63d0\u4e0b\u8fdb\u884c\u7684\u3002\u8fd9\u91cc\u4ee5put\u65b9\u6cd5\u4e3a\u4f8b\u3002<\/p>\n<p>\u901a\u8fc7 <code>put<\/code> \u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u7b49\u5f85\uff0c\u76f4\u5230\u6709\u7a7a\u95f2\u7a7a\u95f4\u53ef\u4ee5\u653e\u5165\u5143\u7d20\u3002<\/p>\n<pre><code class=\"java\">public void put(E e) throws InterruptedException {\n    checkNotNull(e); \/\/ \u4e0d\u5141\u8bb8\u5b58\u7a7a\u503c\uff0cJUC\u4e0b\u7ebf\u7a0b\u5b89\u5168\u7684\u5bb9\u5668\u90fd\u4e0d\u5141\u8bb8\u5b58\u7a7a\u503c\u3002\n\n    \/\/ \u5728JUC\u7684\u5f88\u591a\u7c7b\u91cc\uff0c\u90fd\u4f1a\u770b\u5230\u8fd9\u79cd\u5199\u6cd5\uff1a\u628a\u7c7b\u7684\u5c5e\u6027\u8d4b\u503c\u7ed9\u65b9\u6cd5\u5185\u7684\u4e00\u4e2a\u53d8\u91cf\u3002\n    \/\/ \u8fd9\u662f\u56e0\u4e3a\u7c7b\u7684\u5c5e\u6027\u662f\u5b58\u653e\u5728\u5806\u91cc\u7684\uff0c\u65b9\u6cd5\u5185\u7684\u53d8\u91cf\u662f\u5b58\u653e\u5728\u65b9\u6cd5\u6808\u4e0a\u7684\uff0c\u8bbf\u95ee\u65b9\u6cd5\u6808\u6bd4\u8bbf\u95ee\u5806\u8981\u5feb\u3002\n    \/\/ \u5728\u8fd9\u91cc\uff0cthis.lock\u5c5e\u6027\u8981\u8bbf\u95ee\u4e24\u6b21\uff0c\u901a\u8fc7\u8d4b\u503c\u7ed9\u65b9\u6cd5\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5c31\u8282\u7701\u4e86\u4e00\u6b21\u5806\u7684\u8bbf\u95ee\u3002\n    \/\/ \u5176\u4ed6\u7684\u7c7b\u5c5e\u6027\u53ea\u8bbf\u95ee\u4e00\u6b21\u5c31\u4e0d\u9700\u8981\u8fd9\u6837\u5904\u7406\u4e86\u3002\u4f18\u5316\u65e0\u5904\u4e0d\u5728\uff01\uff01\n    final ReentrantLock lock = this.lock;\n\n    lock.lockInterruptibly();  \/\/ \u52a0\u9501\n    try {\n      \/\/ \u653e\u5728\u5faa\u73af\u91cc\u662f\u907f\u514d\u865a\u5047\u5524\u9192\n      \/\/ \u5bb9\u5668\u6ee1\u7684\u65f6\u5019\u6301\u7eed\u7b49\u5f85\n        while (count == items.length)\n             \/\/ await \u65b9\u6cd5\u4f1a\u5bfc\u81f4\u5f53\u524d\u7ebf\u7a0b\u91ca\u653e\u9501\uff0c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5524\u9192\uff0c\u5524\u9192\u540e\u91cd\u65b0\u83b7\u53d6\u9501\n            notFull.await();\n\n        insert(e);\n    } finally {\n        lock.unlock();  \/\/ \u91ca\u653e\u9501\n    }\n}\n<\/code><\/pre>\n<h4>\u8fed\u4ee3<\/h4>\n<p>\u6839\u636eDOC\u6587\u6863\u8bf4\u660e\uff0c\u6b64\u7c7b <code>iterator()<\/code> \u65b9\u6cd5\u6bcf\u6b21\u8fd4\u56de\u7684 <code>Iterator<\/code> \u662f\u4e00\u4e2a\u201c\u5f31\u4e00\u81f4\u201d\u7684\u8fed\u4ee3\u5668\uff0c\u4ece\u4e0d\u629b\u51fa <code>ConcurrentModificationException<\/code>\uff0c\u5e76\u4e14\u786e\u4fdd\u53ef\u904d\u5386\u8fed\u4ee3\u5668\u6784\u9020\u65f6\u5b58\u5728\u7684\u5143\u7d20\uff0c\u6b64\u5916\u8fd8\u53ef\u80fd\uff08\u4f46\u5e76\u4e0d\u4fdd\u8bc1\uff09\u53cd\u6620\u6784\u9020\u540e\u7684\u6240\u6709\u4fee\u6539\u3002<\/p>\n<p>\u5185\u90e8\u7684\u8fed\u4ee3\u5668\u7c7b\uff1a<\/p>\n<pre><code class=\"java\">private class Itr implements Iterator&lt;E&gt; {\n    private int remaining; \/\/ \u5269\u4f59\u53ef\u8fd4\u56de\u5143\u7d20\u6570\u91cf\n    private int nextIndex; \/\/ \u4e0b\u4e00\u4e2a\u53ef\u8fd4\u56de\u5143\u7d20\u7684\u4f4d\u7f6e\n    private E nextItem;    \/\/ \u4e0b\u4e00\u4e2a\u53ef\u8fd4\u56de\u7684\u5143\u7d20\n    private E lastItem;    \/\/ \u4e0a\u4e00\u6b21\u8fd4\u56de\u7684\u5143\u7d20\n    private int lastRet;   \/\/ \u4e0a\u6b21\u8fd4\u56de\u7684\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c-1 \u8868\u793a\u6ca1\u6709\u3002\n\n    Itr() {\n        final ReentrantLock lock = ArrayBlockingQueue. this.lock;\n        lock.lock();\n        try {\n            lastRet = -1;\n            \/\/ \u521d\u59cb\u5316\u7684\u65f6\u5019\u8bb0\u5f55\u5bb9\u5668\u7684\u5f53\u524d\u5b58\u5728\u5143\u7d20\uff08\u901a\u8fc7\u8bb0\u5f55\u6570  count \u548c\u8bfb\u6307\u9488 takeIndex\uff09\u3002\n            if ((remaining = count) &gt; 0)\n                nextItem = itemAt(nextIndex = takeIndex);\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public boolean hasNext() {\n        return remaining &gt; 0;\n    }\n\n    public E next() {\n        final ReentrantLock lock = ArrayBlockingQueue. this.lock;\n        lock.lock();\n        try {\n            if (remaining &lt;= 0)\n                throw new NoSuchElementException();\n\n            lastRet = nextIndex;\n\n            E x = itemAt(nextIndex);  \/\/ \u68c0\u67e5\u5143\u7d20\u662f\u5426\u88ab\u66f4\u65b0\n            if (x == null) { \/\/ \u8be5\u4f4d\u7f6e\u5143\u7d20\u88ab\u79fb\u9664\u4e86\n                x = nextItem;         \/\/ \u53ea\u80fd\u88ab\u8feb\u8fd4\u56de\u65e7\u503c\n                lastItem = null ;      \/\/ \u7528\u4e8e\u4f7f\u79fb\u9664\u64cd\u4f5c\u5931\u8d25\n            } else {\n                lastItem = x;\n            }\n\n            \/\/ \u8df3\u8fc7\u7a7a\u5143\u7d20\uff08\u4e5f\u5c31\u662f\u8fed\u4ee3\u5668\u521b\u5efa\u4e4b\u540e\u88ab\u79fb\u9664\u7684\u5143\u7d20\uff09\n            while (--remaining &gt; 0 &amp;&amp; \/\/ skip over nulls\n                   (nextItem = itemAt(nextIndex = inc(nextIndex))) == null )\n                   \/\/ \u8bbe\u7f6e\u4e0b\u4e00\u6b21\u8981\u8fd4\u56de\u7684\u5143\u7d20\n                ;\n\n            return x;\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void remove() {\n        final ReentrantLock lock = ArrayBlockingQueue. this.lock;\n        lock.lock();\n        try {\n            int i = lastRet;\n            if (i == -1)\n                throw new IllegalStateException();\n            lastRet = -1;\n            E x = lastItem;\n            lastItem = null ;\n            \/\/ only remove if item still at index\n            if (x != null &amp;&amp; x == items[i]) {\n                  \/\/ \u53ea\u6709\u5728\u4e0a\u6b21\u8c03\u7528 next() \u65b9\u6cd5\u65f6\u5019\u5f53\u524d\u904d\u5386\u4f4d\u7f6e\u7684\u5143\u7d20\u4ecd\u7136\u5728\u90a3\u91cc\u65f6\u624d\u79fb\u9664\n                boolean removingHead = (i == takeIndex);\n                removeAt(i);\n                if (!removingHead)\n                    nextIndex = dec(nextIndex);\n            }\n        } finally {\n            lock.unlock();\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u4ece\u8fed\u4ee3\u5668\u7684\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u5b83\u53ea\u80fd\u611f\u77e5\u5728\u5b83\u521b\u5efa\u65f6\u6709\u6548\u5143\u7d20\u4f4d\u7f6e\u4e0a\u7684\u53d8\u5316\uff08\u5220\u9664\u3001\u88ab\u66ff\u6362\uff09\uff0c\u800c\u4e0d\u80fd\u611f\u77e5\u65b0\u589e\u7684\u5143\u7d20\u3002<\/p>\n<h2>\u5173\u4e8e\u7ebf\u7a0b\u5b89\u5168\u7c7b\u7684\u4f7f\u7528\u6ce8\u610f<\/h2>\n<p>\u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u8df5\u300b\u63d0\u5230\uff1a\u5c31\u7b97\u4ee3\u7801\u90fd\u662f\u7528\u57fa\u4e8e\u7ebf\u7a0b\u5b89\u5168\u7c7b\u6784\u5efa\u7684\u7a0b\u5e8f\u4e5f\u4e0d\u4e00\u5b9a\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<pre><code class=\"java\">static ArrayBlockingQueue&lt;Integer&gt; queue = new ArrayBlockingQueue&lt;Integer&gt;(10);\n\n\nfor (Iterator&lt;Integer&gt; iterator = queue.iterator(); iterator.hasNext(); ) {\n      iterator.next();\n      iterator.remove();\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u91cc\uff0c\u5982\u679c <code>queue<\/code> \u662f\u53ef\u4ee5\u88ab\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u7684\uff0c\u90a3\u4e48\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u56e0\u4e3a iterator \u521b\u5efa\u4e4b\u540e\uff0chasNext\u3001next\u3001remove \u8fd9\u4e09\u4e2a\u65b9\u6cd5\u7684\u8c03\u7528\u4e4b\u95f4\uff0c\u5c31\u6709\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u4e86 queue \uff0c\u5bfc\u81f4\u629b\u51fa\u5f02\u5e38 NoSuchElementException\u3002<\/p>\n<p>\u7ebf\u7a0b\u5b89\u5168\u7c7b\u53ea\u4fdd\u8bc1\u4e86\u5b83\u7684\u5355\u4e2a\u65b9\u6cd5\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5982\u679c\u8981\u786e\u4fdd\u591a\u4e2a\u65b9\u6cd5\u8c03\u7528\u4e4b\u95f4\u8fd8\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u540c\u6b65\u8fdb\u884c\u4fdd\u8bc1\uff0c\u800c\u4e14\u8981\u7528\u540c\u4e00\u4e2a\u9501\u6765\u4fdd\u62a4\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"java\">synchronized (queue) {\n       for (Iterator&lt;Integer&gt; iterator = queue.iterator(); iterator.hasNext(); ) {\n            iterator.next();\n            iterator.remove();\n      }\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>java.util.concurrent.ArrayBlockingQueue  &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131225\/682\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[142,110,143],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/682"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/682\/revisions\/684"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}