{"id":1985,"date":"2019-10-22T08:20:37","date_gmt":"2019-10-22T00:20:37","guid":{"rendered":"https:\/\/coderbee.net\/?p=1985"},"modified":"2019-10-21T22:25:11","modified_gmt":"2019-10-21T14:25:11","slug":"juc-%e5%b9%b6%e5%8f%91-queue-%e8%ae%be%e8%ae%a1%e4%b8%8e%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20191022\/1985","title":{"rendered":"JUC \u5e76\u53d1 Queue \u8bbe\u8ba1\u4e0e\u4ecb\u7ecd"},"content":{"rendered":"<h1>Queue \u4f53\u7cfb<\/h1>\n<p>Queue \u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u961f\u5217\u3002<\/p>\n<p>ArrayBlockingQueue \u548c LinkedBlockingQueue \u662f\u5e26\u963b\u585e\u7279\u6027\uff0c\u57fa\u4e8e\u9501\u6765\u5b9e\u73b0\u3002ArrayBlockingQueue \u91c7\u7528\u540c\u4e00\u628a\u9501\u6765\u63a7\u5236\u51fa\u3001\u5165\u961f\u5217\u64cd\u4f5c\uff1bLinkedBlockingQueue \u7528\u4e24\u628a\u9501\u6765\u5206\u522b\u63a7\u5236\u51fa\u3001\u5165\u961f\u5217\u64cd\u4f5c\uff0c\u63d0\u9ad8\u4e86\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n<p>ConcurrentLinkedQueue \u975e\u963b\u585e\uff0c\u91c7\u7528\u65e0\u9501\u7b97\u6cd5\u3001\u5229\u7528 CAS \u64cd\u4f5c\u6765\u5b9e\u73b0\u3002<\/p>\n<h2>0.1 BlockingQueue<\/h2>\n<p>\u5f53\u751f\u4ea7\u8005\u5411\u961f\u5217\u6dfb\u52a0\u5143\u7d20\u4f46\u961f\u5217\u5df2\u6ee1\u65f6\uff0c\u751f\u4ea7\u8005\u4f1a\u88ab\u963b\u585e\uff1b\u5f53\u6d88\u8d39\u8005\u4ece\u961f\u5217\u79fb\u9664\u5143\u7d20\u3001\u4f46\u961f\u5217\u4e3a\u7a7a\u65f6\uff0c\u6d88\u8d39\u8005\u4f1a\u88ab\u963b\u585e\u3002<\/p>\n<p>\u5176\u5b9e\u73b0\u7c7b\u5fc5\u987b\u662f\u7ebf\u7a0b\u5b89\u5168\uff0c\u5165\u961f\u5217 happen-before \u51fa\u961f\u5217\u3002<\/p>\n<h2>0.2 TransferQueue<\/h2>\n<p>\u7ee7\u627f\u81ea BlockingQueue\uff0c\u66f4\u8fdb\u4e00\u6b65\uff1a\u751f\u4ea7\u8005\u4f1a\u4e00\u76f4\u963b\u585e\u76f4\u5230\u6dfb\u52a0\u5230\u961f\u5217\u7684\u5143\u7d20\u88ab\u67d0\u4e00\u4e2a\u6d88\u8d39\u8005\u6240\u6d88\u8d39\uff08\u4e0d\u4ec5\u4ec5\u662f\u6dfb\u52a0\u5230\u961f\u5217\uff09\u3002<\/p>\n<p>\u7279\u522b\u9002\u7528\u4e8e\u8fd9\u79cd\u5e94\u7528\u95f4\u4f20\u9012\u6d88\u606f\u7684\u573a\u666f\uff1a\u751f\u4ea7\u8005\u6709\u65f6\u9700\u8981\u7b49\u5f85\u6d88\u8d39\u8005\u63a5\u6536\u6d88\u606f\uff0c\u6709\u65f6\u53ea\u9700\u628a\u6d88\u606f\u653e\u8fdb\u961f\u5217\u3001\u4e0d\u9700\u8981\u7b49\u5f85\u6d88\u8d39\u8005\u63a5\u6536\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u4f20\u9012\u5143\u7d20\u7ed9\u6d88\u8d39\u8005\uff0c\u5982\u679c\u9700\u8981\u5219\u7b49\u5f85\u3002\u786e\u4fdd\u4e00\u6b21\u4f20\u9012\u5b8c\u6210\u3002\nvoid transfer(E e);\n\n\/\/ \u975e\u963b\u585e\nboolean tryTransfer(E e);\n\n\/\/ \u57fa\u4e8e\u7b49\u5f85\u65f6\u95f4\u7684\u3002\nboolean tryTransfer(E e, long timeout, TimeUnit unit);\n\n\/\/ \u8fd4\u56de\u662f\u5426\u6709\u5728\u7b49\u5f85\u63a5\u6536\u5143\u7d20\u7684\u6d88\u8d39\u8005\n\/\/ \uff08BlockingQueue.take()\u6216\u5e26\u7b49\u5f85\u65f6\u95f4\u7684 poll \u65b9\u6cd5\u8c03\u7528\uff09\nboolean hasWaitingConsumer();\n\n\/\/ \u8fd4\u56de\u5927\u6982\u7684\u5728\u7b49\u5f85\u63a5\u6536\u5143\u7d20\u7684\u6d88\u8d39\u8005\n\/\/\uff08BlockingQueue.take()\u6216\u5e26\u7b49\u5f85\u65f6\u95f4\u7684 poll \u65b9\u6cd5\u8c03\u7528\uff09\nint getWaitingConsumerCount();\n<\/code><\/pre>\n<h2>0.3 Deque<\/h2>\n<p>\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u63d2\u5165\u3001\u5220\u9664\u5143\u7d20\u7684\u7ebf\u6027\u96c6\u5408\u3002<\/p>\n<p>\u5b9e\u73b0\u7c7b\uff1a<\/p>\n<ul>\n<li>ArrayDeque\uff1a\u57fa\u4e8e\u6570\u7ec4\u52a0\u5934\u5c3e\u4e24\u4e2a\u6307\u9488\u6765\u5b9e\u73b0\u3001\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<li>LinkedList\uff1a\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u3001\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<li>ConcurrentLinkedDeque\uff1a\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u3001CAS \u5143\u8bed\u5b9e\u73b0\u3001\u65e0\u754c\u7684\u3002<\/li>\n<\/ul>\n<h2>0.4 BlockingDeque<\/h2>\n<p>\u5f53 Deque \u91cc\u6ca1\u6709\u5143\u7d20\u65f6\u963b\u585e\u6d88\u8d39\u8005\uff0c\u5f53\u6ca1\u6709\u7a7a\u95f2\u7a7a\u95f4\u65f6\u963b\u585e\u751f\u4ea7\u8005\u3002<\/p>\n<p>\u76ee\u524d\u53ea\u6709\u4e00\u4e2a\u5b9e\u73b0\u7c7b LinkedBlockingDeque\uff0c\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u6765\u5b58\u50a8\u5143\u7d20\uff0c\u652f\u6301\u5bb9\u91cf\u9650\u5236\uff0c\u7528\u4e00\u628a\u9501\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u56e0\u4e3a\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u64cd\u4f5c\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5408\u9002\u3002<\/p>\n<p><!--more--><\/p>\n<h1>1. ArrayBlockingQueue<\/h1>\n<h2>1.1 \u8bbe\u8ba1\u601d\u60f3<\/h2>\n<p>ArrayBlockingQueue \u7531 Array\u3001Blocking\u3001Queue \u7ec4\u6210\uff0c\u5f52\u6839\u7ed3\u5e95\u662f Queue \u3002<\/p>\n<p>Array \u8868\u660e\u5b83\u662f\u57fa\u4e8e\u6570\u7ec4\u6765\u5b58\u50a8\u7684\uff0cBlocking \u8868\u793a\u961f\u5217\u5b58\u653e\u4e0d\u4e0b\u65f6\u53ef\u4ee5\u963b\u585e\u5165\u961f\u5217\u8c03\u7528\u8005\u3001\u4e3a\u7a7a\u65f6\u963b\u585e\u51fa\u961f\u5217\u8c03\u7528\u8005\uff0cQueue \u8868\u793a\u8fd9\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u961f\u5217\u3002<\/p>\n<p>\u961f\u5217\u662f\u5148\u8fdb\u5148\u51fa\u7684\uff0c\u57fa\u4e8e\u6570\u7ec4\u6765\u5b58\u50a8\uff0c\u90a3\u4e48\u7b2c\u4e00\u4e2a\u8fdb\u961f\u5217\u7684\u653e\u5728\u4e0b\u6807 0 \u4f4d\u7f6e\uff0c\u7b2c\u4e8c\u4e2a\u653e\u5728\u4e0b\u6807 1 \u4f4d\u7f6e\uff0c\uff0c\uff0c\u51fa\u961f\u5217\u7684\u65f6\u5019\uff0c\u4e5f\u662f\u4ece\u4e0b\u6807 0 \u5f00\u59cb\uff0c\u8fd9\u65f6\uff0c\u5982\u679c\u50cf ArrayList \u90a3\u6837\uff0c\u628a\u540e\u7eed\u7684\u5143\u7d20\u5f80\u524d\u62f7\u8d1d\u4f1a\u5e26\u6765\u5f88\u5927\u7684\u6027\u80fd\u5f00\u9500\uff0c\u8fd9\u4e2a\u5b9e\u73b0\u601d\u8def\u662f\u4e0d\u73b0\u5b9e\u7684\u3002<\/p>\n<p>\u6211\u4eec\u4e0d\u80fd\u91c7\u7528\u79fb\u52a8\u5143\u7d20\u7684\u65b9\u5f0f\u6765\u5904\u7406\u51fa\u961f\u5217\u3002\u90a3\u4e48\u6362\u79cd\u601d\u8def\uff0c\u7528\u6307\u9488\u6765\u6807\u8bb0\u53ef\u4ee5\u8bfb\u53d6\u3001\u5199\u5165\u7684\u4f4d\u7f6e\uff0c\u5728\u8bfb\u5199\u65f6\u53ea\u9700\u4fee\u6539\u6307\u9488\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<pre><code>          takeIndex                           putIndex\n|-------|-----------|-------|-------|-------|----------|-------|\n|       |-----5-----|---4---|---3---|---3---|          |       |\n|-------|-----------|-------|-------|-------|----------|-------|\n<\/code><\/pre>\n<p>takeIndex \u6307\u5411\u53ef\u51fa\u961f\u5217\u7684\u4f4d\u7f6e\uff0cputIndex \u6307\u5411\u53ef\u4ee5\u5199\u5165\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u7531\u4e8e\u6570\u7ec4\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\uff0c\u9700\u8981\u4e25\u683c\u63a7\u5236\u8fdb\u5165\u7684\u961f\u5217\u7684\u6570\u91cf\uff0c\u9700\u8981\u7ef4\u62a4\u5143\u7d20\u7684\u6570\u91cf count \u3002<code>count == 0<\/code> \u8868\u793a\u6ca1\u6709\u5143\u7d20\u53ef\u51fa\u961f\u5217\uff0c<code>count == items.length<\/code> \u8868\u793a\u6ee1\uff0c\u6ca1\u6709\u7a7a\u95f4\u53ef\u4ee5\u5165\u961f\u5217\u3002<\/p>\n<p>\u4e00\u4e2a\u5165\u961f\u5217\u64cd\u4f5c\u6d89\u53ca\uff1a\u66f4\u65b0 array[putIndex] \u7684\u5185\u5bb9\u3001\u66f4\u65b0 putIndex\u3001count \u52a01 \u3002\u6d89\u53ca\u7684\u6b65\u9aa4\u591a\uff0c\u91c7\u7528\u52a0\u9501\u7684\u65b9\u5f0f\u662f\u66f4\u5408\u9002\u7684\u3002<\/p>\n<h3>1.1.0 \u8bbe\u8ba1\u5c0f\u7ed3<\/h3>\n<ol>\n<li>\u7528\u6570\u7ec4\u548c\u51fa\u3001\u5165\u4e24\u4e2a\u6307\u9488\u6765\u5b9e\u73b0\u9ad8\u6548\u8bbf\u95ee\u3002<\/li>\n<li>\u7528\u9501\u6765\u5b9e\u73b0\u961f\u5217\u8bbf\u95ee\u7684\u7ebf\u7a0b\u5b89\u5168\u3001\u516c\u5e73\u6027\u3002<\/li>\n<li>\u5229\u7528\u9501\u5173\u8054\u7684\u4e24\u4e2a\u6761\u4ef6 <code>notEmpty\/notFull<\/code> \u6765\u5b9e\u73b0\u963b\u585e\u548c\u5524\u9192\u3002<\/li>\n<li>\u7ef4\u62a4\u4e00\u4e2a\u666e\u901a\u6574\u578b\u53d8\u91cf <code>count<\/code> \u6765\u65b9\u4fbf\u7edf\u8ba1\u961f\u5217\u91cc\u7684\u5143\u7d20\u6570\u91cf\uff0c\u540c\u65f6\u7528\u4e8e\u534f\u8c03\u51fa\u5165\u64cd\u4f5c\uff08\u5bf9 count \u8fdb\u884c\u5224\u65ad\uff09\uff0c\u975e <code>volatile<\/code> \u4fee\u9970\u7684\uff0c\u56e0\u4e3a\u5728\u540c\u4e00\u4e2a\u9501\u4fdd\u62a4\u4e0b\u8bbf\u95ee\u3002<\/li>\n<\/ol>\n<h1>1.2 \u5b9e\u73b0\u6e90\u7801<\/h1>\n<h2>1.2.1 \u6570\u636e\u6a21\u578b<\/h2>\n<pre><code class=\"java\">public class ArrayBlockingQueue&lt;E&gt; extends AbstractQueue&lt;E&gt;\n        implements BlockingQueue&lt;E&gt;, java.io.Serializable {\n\n    \/** The queued items *\/\n    final Object[] items;\n\n    \/** items index for next take, poll, peek or remove *\/\n    int takeIndex;\n\n    \/** items index for next put, offer, or add *\/\n    int putIndex;\n\n    \/** Number of elements in the queue *\/\n    int count;\n\n    final ReentrantLock lock;\n\n    \/** Condition for waiting takes *\/\n    private final Condition notEmpty;\n\n    \/** Condition for waiting puts *\/\n    private final Condition notFull;\n}\n<\/code><\/pre>\n<p>\u51fa\u5165\u961f\u5217\u7684\u64cd\u4f5c\uff0c\u90fd\u5fc5\u987b\u5728<strong>\u6301\u6709\u9501<\/strong>\u7684\u524d\u63d0\u4e0b\u8fdb\u884c\uff1a<\/p>\n<pre><code class=\"java\">private void enqueue(E x) {\n    \/\/ assert lock.getHoldCount() == 1;\n    \/\/ assert items[putIndex] == null;\n    final Object[] items = this.items;\n    items[putIndex] = x;\n    if (++putIndex == items.length)\n        putIndex = 0;\n    count++;\n    notEmpty.signal();\n}\n\nprivate E dequeue() {\n    \/\/ assert lock.getHoldCount() == 1;\n    \/\/ assert items[takeIndex] != null;\n    final Object[] items = this.items;\n    @SuppressWarnings(\"unchecked\")\n    E x = (E) items[takeIndex];\n    items[takeIndex] = null;\n    if (++takeIndex == items.length)\n        takeIndex = 0;\n    count--;\n    if (itrs != null)\n        itrs.elementDequeued();\n    notFull.signal();\n    return x;\n}\n<\/code><\/pre>\n<p>\u5177\u4f53\u7684\u6e90\u7801\u89e3\u91ca\u89c1 <a href=\"https:\/\/coderbee.net\/?p=682\">JUC ArrayBlockingQueue<\/a> \u3002<\/p>\n<h1>2. LinkedBlockingQueue<\/h1>\n<p>\u5982\u679c\u51fa\u3001\u5165\u961f\u5217\u51e0\u4e4e\u4ee5\u540c\u6837\u7684\u901f\u7387\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u4e24\u8005\u4e4b\u95f4\u5176\u5b9e\u662f\u6ca1\u6709\u7ade\u4e89\u7684\uff0c\u53ef\u4ee5\u7528\u4e0d\u540c\u7684\u9501\u6765\u5206\u522b\u4fdd\u62a4\u51fa\u3001\u5165\u64cd\u4f5c\uff0c\u4e24\u79cd\u64cd\u4f5c\u4e4b\u95f4\u901a\u8fc7\u539f\u5b50\u53d8\u91cf\u6765\u611f\u77e5\u3002<\/p>\n<p>LinkedBlockingQueue \u662f\u57fa\u4e8e\u94fe\u8868\u7684\u3001\u5bb9\u91cf\u4efb\u610f\uff08\u6700\u5927 Integer.MAX_VALUE\uff09\u7684\u963b\u585e\u961f\u5217\u3002<\/p>\n<p>\u5b9e\u73b0\u4e0a\uff1a\u91c7\u7528\u4e24\u628a\u9501\u5206\u522b\u7528\u4e8e\u63a7\u5236\u5165\u961f\u5217\u3001\u51fa\u961f\u5217\uff0c\u51fa\u5165\u961f\u5217\u4e4b\u95f4\u901a\u8fc7\u4e00\u4e2a\u539f\u5b50\u53d8\u91cf count \u6765\u534f\u8c03\u3002\u5982\u679c count \u7b49\u4e8e 0 \u5219\u8ba4\u4e3a\u6ca1\u6709\u53ef\u4ee5\u51fa\u961f\u5217\u7684\u5143\u7d20\uff0c\u9700\u8981\u7b49\u5f85\uff1b\u5982\u679c count \u7b49\u4e8e\u5bb9\u91cf\uff0c\u662f\u6ca1\u6709\u5269\u4f59\u7a7a\u95f4\u53ef\u4ee5\u5b58\u653e\u5143\u7d20\uff0c\u5165\u961f\u5217\u64cd\u4f5c\u9700\u8981\u963b\u585e\u7b49\u5f85\u3002<\/p>\n<p>\u8fd9\u4e2a\u7684\u5b9e\u73b0\u6709\u70b9\u7c7b\u4f3c\u4e8e\u4e24\u9636\u6bb5\u63d0\u4ea4\uff0c\u4ee5\u5165\u961f\u5217\u4e3a\u4f8b\uff0c\u9996\u5148\u83b7\u53d6\u5165\u961f\u5217\u7684\u9501 putLock\uff1a<br \/>\n1. \u8fdb\u884c\u94fe\u8868\u961f\u5217\uff0c\u6b64\u65f6\u539f\u5b50\u53d8\u91cf count \u6ca1\u53d8\uff0c\u51fa\u961f\u5217\u64cd\u4f5c\u662f\u770b\u4e0d\u5230\u7684\uff1b<br \/>\n2. \u539f\u5b50\u53d8\u91cf count \u52a0 1\uff0c\u8fd9\u4e00\u6b65\u76f8\u5f53\u4e8e\u4e8b\u52a1 commit\uff0c\u51fa\u961f\u5217\u64cd\u4f5c\u53ef\u4ee5\u770b\u5230\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u662f\u51fa\u5165\u961f\u5217\u64cd\u4f5c\uff0c\u6bd4\u5982\u5224\u7b49\u67d0\u4e2a\u5bf9\u8c61\u662f\u5426\u5728\u961f\u5217\u91cc\uff0c\u5219\u9700\u8981\u540c\u65f6\u52a0\u4e24\u628a\u9501\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u6027\u3002<\/p>\n<p>LinkedBlockingQueue \u7684\u541e\u5410\u91cf\u901a\u5e38\u8981\u9ad8\u4e8e ArrayBlockingQueue \u961f\u5217\uff0c\u4f46\u662f\u5728\u5927\u591a\u6570\u5e76\u53d1\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5176\u53ef\u9884\u77e5\u7684\u6027\u80fd\u8981\u4f4e\u3002<\/p>\n<p>\u51fa\u5165\u961f\u5217\u7684\u64cd\u4f5c\uff0c\u5fc5\u987b\u5728<strong>\u6301\u6709 putLock \u6216 takeLock \u9501<\/strong>\u7684\u524d\u63d0\u4e0b\u8fdb\u884c\uff1a<\/p>\n<pre><code class=\"java\">private void enqueue(Node&lt;E&gt; node) {\n    \/\/ assert putLock.isHeldByCurrentThread();\n    \/\/ assert last.next == null;\n    last = last.next = node;\n}\n\nprivate E dequeue() {\n    \/\/ assert takeLock.isHeldByCurrentThread();\n    \/\/ assert head.item == null;\n    Node&lt;E&gt; h = head;\n    Node&lt;E&gt; first = h.next;\n    h.next = h; \/\/ help GC\n    head = first;\n    E x = first.item;\n    first.item = null;\n    return x;\n}\n\npublic boolean offer(E e) {\n    if (e == null) throw new NullPointerException();\n    final AtomicInteger count = this.count;\n    if (count.get() == capacity)\n        return false;\n    int c = -1;\n    Node&lt;E&gt; node = new Node&lt;E&gt;(e);\n    final ReentrantLock putLock = this.putLock;\n    putLock.lock();\n    try {\n        if (count.get() &lt; capacity) {\n            enqueue(node);\n            c = count.getAndIncrement();\n            if (c + 1 &lt; capacity)\n                notFull.signal();\n        }\n    } finally {\n        putLock.unlock();\n    }\n    if (c == 0)\n        signalNotEmpty();\n    return c &gt;= 0;\n}\n\npublic E take() throws InterruptedException {\n    E x;\n    int c = -1;\n    final AtomicInteger count = this.count;\n    final ReentrantLock takeLock = this.takeLock;\n    takeLock.lockInterruptibly();\n    try {\n        while (count.get() == 0) {\n            notEmpty.await();\n        }\n        x = dequeue();\n        c = count.getAndDecrement();\n        if (c &gt; 1)\n            notEmpty.signal();\n    } finally {\n        takeLock.unlock();\n    }\n    if (c == capacity)\n        signalNotFull();\n    return x;\n}\n<\/code><\/pre>\n<p>\u5177\u4f53\u7684\u6e90\u7801\u89e3\u91ca\u89c1 <a href=\"https:\/\/coderbee.net\/?p=685\">JUC LinkedBlockingQueue<\/a> \u3002<\/p>\n<h1>3. ConcurrentLinkedQueue<\/h1>\n<h2>3.1 \u8bbe\u8ba1\u6298\u8877<\/h2>\n<p>\u5982\u679c\u5e76\u53d1\u8fdb\u4e00\u6b65\u63d0\u9ad8\uff0c\u51fa\u5165\u961f\u5217\u7684\u64cd\u4f5c\u90fd\u5f88\u9891\u7e41\uff0c\u90a3\u4e48\u7ebf\u7a0b\u57fa\u672c\u4e0a\u662f\u4e0d\u9700\u8981\u7b49\u5f85\u6216\u8005\u7b49\u5f85\u65f6\u95f4\u6781\u77ed\u3002\u963b\u585e\u4e00\u822c\u8981\u501f\u52a9\u9501\uff0c\u9501\u53c8\u4f1a\u660e\u663e\u9650\u5236\u5e76\u53d1\u80fd\u529b\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u653e\u5f03\u963b\u585e\u7684\u7279\u6027\u3002<\/p>\n<p>\u5e76\u53d1\u7a0b\u5e8f\u8bbe\u8ba1\uff0c\u96be\u5728\u5171\u4eab\u72b6\u6001\u7684\u7ba1\u7406\u3002\u5982\u679c\u6ca1\u6709\u5171\u4eab\u72b6\u6001\uff0c\u90a3\u4e48\u5929\u7136\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u53ef\u4ee5\u5c3d\u91cf\u51cf\u5c11\u5171\u4eab\u72b6\u6001\u6765\u7b80\u5316\u7a0b\u5e8f\u3002<\/p>\n<p>\u5728 ArrayBlockingQueue \u548c LinkedBlockingQueue \u91cc\u90fd\u6d89\u53ca\u8868\u793a\u961f\u5217\u91cc\u5143\u7d20\u6570\u91cf\u7684\u5171\u4eab\u72b6\u6001 count \uff0c\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u7684\u961f\u5217\uff0c\u6211\u4eec\u5176\u5b9e\u4e0d\u662f\u90a3\u4e48\u5173\u5fc3\u67d0\u4e00\u65f6\u523b\u961f\u5217\u91cc\u5143\u7d20\u7684\u7cbe\u786e\u6570\u91cf\u3002\u4e3a\u4e86\u9ad8\u5e76\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u653e\u5f03\u8fd9\u4e2a count\uff0c\u901a\u8fc7\u8fed\u4ee3\u6765\u7edf\u8ba1\u4e00\u4e2a\u4e0d\u90a3\u4e48\u51c6\u786e\u7684\u6570\u91cf\u3002<\/p>\n<p>\u5982\u679c\u91c7\u7528\u57fa\u4e8e\u6570\u7ec4\u52a0\u4e24\u4e2a\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4e24\u4e2a\u6307\u9488\u7684\u5224\u65ad\u4e0e\u66f4\u65b0\u5c31\u4f1a\u5f62\u6210\u4e00\u4e2a\u540c\u6b65\u533a\uff0c\u5f88\u96be\u7528 CAS \u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u57fa\u4e8e\u94fe\u8868\u6765\u5b9e\u73b0\u4e5f\u4e5f\u6ca1\u6cd5\u50cf LinkedBlockingQueue \u505a\u5230\u4e00\u81f4\u7684\u72b6\u6001\uff0c<code>ConcurrentLinkedQueue<\/code> \u5141\u8bb8\u4e2d\u95f4\u8fc7\u7a0b\u51fa\u73b0\u4e00\u4e9b\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u4f46\u80fd\u8fbe\u5230\u6700\u7ec8\u4e00\u81f4\u3002<\/p>\n<h1>3.2 \u5b9e\u73b0<\/h1>\n<p><code>ConcurrentLinkedQueue<\/code> \u662f\u57fa\u4e8e\u94fe\u8868\u7684\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u3001\u65e0\u8fb9\u754c \u961f\u5217\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u65e0\u8fb9\u754c\u7684\u961f\u5217\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u8017\u5c3d\u3002<\/p>\n<p>\u8be5\u961f\u5217\u5b9e\u73b0\u53c2\u8003\u4e86 Maged M. Michael \u548c Michael L. Scott \u63d0\u51fa\u7684\u7b97\u6cd5\uff1a\u300aFast, and Practical Non-Blocking and Blocking Concurrent Queue<br \/>\n Algorithms\u300b\u3002<\/p>\n<p><code>ConcurrentLinkedQueue<\/code> \u91c7\u7528\u7684\u662f\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\uff0c\u4e3a\u65b9\u4fbf\u51fa\u3001\u5165\u961f\u5217\uff0c\u7ef4\u62a4\u4e86 <code>head\/tail<\/code> \u4e24\u4e2a\u6307\u9488\u6307\u5411\u961f\u5217\u7684\u5934\u548c\u5c3e\u3002<\/p>\n<p>\u5934\u5c3e\u7ed3\u70b9\u662f\u8981\u9075\u5b88\u4e00\u5b9a\u7684\u89c4\u5219\u548c\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\uff0c\u5177\u4f53\u89c1 <a href=\"https:\/\/coderbee.net\/?p=701\">JUC ConcurrentLinkedQueue<\/a> \u3002<\/p>\n<pre><code class=\"java\">public class ConcurrentLinkedQueue&lt;E&gt; extends AbstractQueue&lt;E&gt;\n        implements Queue&lt;E&gt;, java.io.Serializable {\n\n    private static class Node&lt;E&gt; {\n        volatile E item;\n        volatile Node&lt;E&gt; next;\n    }\n\n    private transient volatile Node&lt;E&gt; head;\n    private transient volatile Node&lt;E&gt; tail;\n}\n<\/code><\/pre>\n<h1>4. \u5bf9\u6bd4<\/h1>\n<table>\n<thead>\n<tr>\n<th>\u7c7b<\/th>\n<th>\u662f\u5426\u57fa\u4e8e\u9501<\/th>\n<th>\u5e95\u5c42\u5b58\u50a8<\/th>\n<th>\u5bb9\u91cf<\/th>\n<th>\u4f7f\u7528\u573a\u666f\/\u5907\u6ce8<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ArrayBlockingQueue<\/td>\n<td>\u57fa\u4e8e\u4e00\u628a\u91cd\u5165\u9501<\/td>\n<td>\u6570\u7ec4+\u51fa\u3001\u5165\u6307\u9488<\/td>\n<td>\u56fa\u5b9a\u5bb9\u91cf<\/td>\n<td>\u9002\u7528\u4e8e\u51fa\u5165\u961f\u5217\u5e76\u53d1\u4e0d\u9ad8\u7684\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td>LinkedBlockingQueue<\/td>\n<td>\u51fa\u3001\u5165\u961f\u5217\u5404\u7528\u4e00\u628a\u91cd\u5165\u9501\uff0c\u7528\u539f\u5b50\u53d8\u91cf\u534f\u8c03<\/td>\n<td>\u5355\u5411\u94fe\u8868\u3001\u5934\u5c3e\u6307\u9488<\/td>\n<td>\u53ef\u6307\u5b9a\u5bb9\u91cf<\/td>\n<td>Executors.newFixedThreadPool<br \/>\/newSingleThreadExecutor \u65b9\u6cd5\u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td>ConcurrentLinkedQueue<\/td>\n<td>\u65e0\u9501\/CAS<\/td>\n<td>\u5355\u5411\u94fe\u8868\u3001\u5934\u5c3e\u6307\u9488<\/td>\n<td>\u65e0\u754c<\/td>\n<td>\u5141\u8bb8\u4e2d\u95f4\u8fc7\u7a0b\u51fa\u73b0\u4e0d\u4e00\u81f4\uff0cnode.next==null\u4f5c\u4e3a\u5c3e\u7ed3\u70b9\u7684\u5224\u65ad\uff0c\u800c\u4e0d\u662f tail \u6307\u9488<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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<h2><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\" \/><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Queue \u4f53\u7cfb Queue \u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u961f\u5217\u3002 ArrayBlocking &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20191022\/1985\">\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":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[142,328,110,144,331,174],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1985"}],"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=1985"}],"version-history":[{"count":6,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1985\/revisions"}],"predecessor-version":[{"id":1991,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1985\/revisions\/1991"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}