{"id":685,"date":"2013-12-26T16:01:22","date_gmt":"2013-12-26T08:01:22","guid":{"rendered":"http:\/\/coderbee.net\/?p=685"},"modified":"2013-12-26T16:01:22","modified_gmt":"2013-12-26T08:01:22","slug":"juc-linkedblockingqueue","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131226\/685","title":{"rendered":"JUC LinkedBlockingQueue"},"content":{"rendered":"<p><code>java.util.concurrent.LinkedBlockingQueue<\/code> \u662f\u4e00\u4e2a\u57fa\u4e8e\u5355\u5411\u94fe\u8868\u7684\u3001\u8303\u56f4\u4efb\u610f\u7684\uff08\u5176\u5b9e\u662f\u6709\u754c\u7684\uff09\u3001FIFO \u963b\u585e\u961f\u5217\u3002\u8bbf\u95ee\u4e0e\u79fb\u9664\u64cd\u4f5c\u662f\u5728\u961f\u5934\u8fdb\u884c\uff0c\u6dfb\u52a0\u64cd\u4f5c\u662f\u5728\u961f\u5c3e\u8fdb\u884c\uff0c\u5e76\u5206\u522b\u4f7f\u7528\u4e0d\u540c\u7684\u9501\u8fdb\u884c\u4fdd\u62a4\uff0c\u53ea\u6709\u5728\u53ef\u80fd\u6d89\u53ca\u591a\u4e2a\u8282\u70b9\u7684\u64cd\u4f5c\u624d\u540c\u65f6\u5bf9\u4e24\u4e2a\u9501\u8fdb\u884c\u52a0\u9501\u3002<\/p>\n<p>\u961f\u5217\u662f\u5426\u4e3a\u7a7a\u3001\u662f\u5426\u5df2\u6ee1\u4ecd\u7136\u662f\u901a\u8fc7\u5143\u7d20\u6570\u91cf\u7684\u8ba1\u6570\u5668\uff08count\uff09\u8fdb\u884c\u5224\u65ad\u7684\uff0c\u7531\u4e8e\u53ef\u4ee5\u540c\u65f6\u5728\u961f\u5934\u3001\u961f\u5c3e\u5e76\u53d1\u5730\u8fdb\u884c\u8bbf\u95ee\u3001\u6dfb\u52a0\u64cd\u4f5c\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8ba1\u6570\u5668\u5fc5\u987b\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u539f\u5b50\u7c7b <code>AtomicInteger<\/code>\uff0c\u8fd9\u5c31\u51b3\u5b9a\u4e86\u5b83\u7684\u5bb9\u91cf\u8303\u56f4\u662f\uff1a 1 &#8211; Integer.MAX_VALUE\u3002<\/p>\n<p>\u7531\u4e8e\u540c\u65f6\u4f7f\u7528\u4e86\u4e24\u628a\u9501\uff0c\u5728\u9700\u8981\u540c\u65f6\u4f7f\u7528\u4e24\u628a\u9501\u65f6\uff0c\u52a0\u9501\u987a\u5e8f\u4e0e\u91ca\u653e\u987a\u5e8f\u662f\u975e\u5e38\u91cd\u8981\u7684\uff1a\u5fc5\u987b\u4ee5\u56fa\u5b9a\u7684\u987a\u5e8f\u8fdb\u884c\u52a0\u9501\uff0c\u518d\u4ee5\u4e0e\u52a0\u9501\u987a\u5e8f\u7684\u76f8\u53cd\u7684\u987a\u5e8f\u91ca\u653e\u9501\u3002<\/p>\n<p>\u5934\u7ed3\u70b9\u548c\u5c3e\u7ed3\u70b9\u4e00\u5f00\u59cb\u603b\u662f\u6307\u5411\u4e00\u4e2a\u54e8\u5175\u7684\u7ed3\u70b9\uff0c\u5b83\u4e0d\u6301\u6709\u5b9e\u9645\u6570\u636e\uff0c\u5f53\u961f\u5217\u4e2d\u6709\u6570\u636e\u65f6\uff0c\u5934\u7ed3\u70b9\u4ecd\u7136\u6307\u5411\u8fd9\u4e2a\u54e8\u5175\uff0c\u5c3e\u7ed3\u70b9\u6307\u5411\u6709\u6548\u6570\u636e\u7684\u6700\u540e\u4e00\u4e2a\u7ed3\u70b9\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5728\u4e8e\uff0c\u4e0e\u8ba1\u6570\u5668 count \u7ed3\u5408\u540e\uff0c\u5bf9\u961f\u5934\u3001\u961f\u5c3e\u7684\u8bbf\u95ee\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\uff0c\u800c\u4e0d\u9700\u8981\u5224\u65ad\u5934\u7ed3\u70b9\u4e0e\u5c3e\u7ed3\u70b9\u7684\u5173\u7cfb\u3002<br \/>\n<!--more--><\/p>\n<h4>\u5c5e\u6027\u4e0e\u94fe\u8868\u8282\u70b9\u7c7b<\/h4>\n<pre><code class=\"java\">\/\/ \u94fe\u8868\u7684\u7ed3\u70b9\u7c7b\uff0c\u5355\u5411\u94fe\u8868\uff0c\u53ea\u6709\u4e00\u4e2a\u540e\u7ee7\u6307\u9488\nstatic class Node&lt;E&gt; {\n    E item;\n\n     \/*\n     * \u540e\u7ee7\u6307\u9488\u3002\u503c\u4e3a\u4e0b\u5217\u4e4b\u4e00\uff1a\n     * \u5b9e\u9645\u7684\u540e\u7ee7\u7ed3\u70b9\u3002\n     * \u81ea\u8eab\uff0c\u8868\u793a\u540e\u7ee7\u662f head.next \uff08\u7528\u4e8e\u5728\u904d\u5386\u5904\u7406\u65f6\u5224\u65ad\uff09\n     * null\uff0c\u8868\u793a\u6ca1\u6709\u540e\u7ee7\uff08\u8fd9\u662f\u5c3e\u7ed3\u70b9\uff09\n     *\/\n    Node&lt;E&gt; next;\n\n    Node(E x) { item = x; }\n}\n\n\n\/\/ \u6700\u5927\u5bb9\u91cf\u4e0a\u9650\uff0c\u9ed8\u8ba4\u662f Integer.MAX_VALUE\nprivate final int capacity;\n\n\/\/ \u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u8fd9\u662f\u4e2a\u539f\u5b50\u7c7b\u3002\u56e0\u4e3a\u8bfb\u5199\u5206\u522b\u4f7f\u7528\u4e0d\u540c\u7684\u9501\uff0c\u4f46\u90fd\u4f1a\u8bbf\u95ee\u8fd9\u4e2a\u5c5e\u6027\uff0c\u6240\u4ee5\u5b83\u9700\u8981\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\nprivate final AtomicInteger count = new AtomicInteger(0);\n\n\/\/ \u5934\u7ed3\u70b9\nprivate transient Node&lt;E&gt; head;\n\n\/\/ \u5c3e\u7ed3\u70b9\nprivate transient Node&lt;E&gt; last;\n\n\/\/ \u961f\u5934\u8bbf\u95ee\u9501\nprivate final ReentrantLock takeLock = new ReentrantLock();\n\n\/\/ \u961f\u5934\u8bbf\u95ee\u7b49\u5f85\u6761\u4ef6\u3001\u961f\u5217\nprivate final Condition notEmpty = takeLock.newCondition();\n\n\/\/ \u961f\u5c3e\u8bbf\u95ee\u9501\nprivate final ReentrantLock putLock = new ReentrantLock();\n\n\/\/ \u961f\u5c3e\u8bbf\u95ee\u7b49\u5f85\u6761\u4ef6\u3001\u961f\u5217\nprivate final Condition notFull = putLock.newCondition();\n<\/code><\/pre>\n<h4>enqueue \u64cd\u4f5c<\/h4>\n<pre><code class=\"java\">\/\/ \u5728\u6301\u6709 putLock \u9501\u4e0b\u6267\u884c\nprivate void enqueue(Node&lt;E&gt; node) {\n    \/\/ assert putLock.isHeldByCurrentThread();\n    \/\/ assert last.next == null;\n    last = last.next = node;\n}\n<\/code><\/pre>\n<h4>dequeue \u64cd\u4f5c<\/h4>\n<p>\u8fd4\u56de\u961f\u5217\u91cc\u7b2c\u4e00\u4e2a\u6709\u6548\u5143\u7d20\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u5728\u6301\u6709 takeLock \u9501\u4e0b\u6267\u884c\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\n    head = first;\n    E x = first.item;\n    first.item = null; \/\/ \u51fa\u961f\u5217\u540e\u7684\u7ed3\u70b9\u4f5c\u4e3a\u65b0\u7684\u54e8\u5175\u7ed3\u70b9\n    return x;\n}\n<\/code><\/pre>\n<h4>\u5bf9\u4e24\u628a\u9501\u7684\u52a0\u9501\u4e0e\u91ca\u653e<\/h4>\n<p>\u5728\u9700\u8981\u5bf9\u4e24\u628a\u9501\u540c\u65f6\u52a0\u9501\u65f6\uff0c\u628a\u52a0\u9501\u7684\u987a\u5e8f\u4e0e\u91ca\u653e\u7684\u987a\u5e8f\u5c01\u88c5\u6210\u65b9\u6cd5\uff0c\u786e\u4fdd\u6240\u6709\u5730\u65b9\u90fd\u662f\u4e00\u81f4\u7684\u3002\u800c\u4e14\u83b7\u53d6\u9501\u65f6\u90fd\u662f\u4e0d\u54cd\u5e94\u4e2d\u65ad\u7684\uff0c\u4e00\u76f4\u83b7\u53d6\u76f4\u5230\u52a0\u9501\u6210\u529f\uff0c\u8fd9\u5c31\u907f\u514d\u4e86\u7b2c\u4e00\u628a\u9501\u52a0\u9501\u6210\u529f\uff0c\u800c\u7b2c\u4e8c\u628a\u9501\u52a0\u9501\u5931\u8d25\u5bfc\u81f4\u9501\u4e0d\u91ca\u653e\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff0c\u9501\u7684\u91ca\u653e\u987a\u5e8f\u4e0e\u52a0\u9501\u987a\u5e8f\u662f\u76f8\u53cd\u7684\u3002<\/strong><\/p>\n<pre><code class=\"java\">\/\/ \u628a\u56fa\u5b9a\u7684\u52a0\u9501\u987a\u5e8f\u5c01\u88c5\u5728\u65b9\u6cd5\u5185\uff0c\u786e\u4fdd\u6240\u6709\u7684\u5bf9\u4e24\u628a\u9501\u52a0\u9501\u7684\u987a\u5e8f\u90fd\u662f\u4e00\u81f4\u7684\u3002\nvoid fullyLock() {\n    putLock.lock();\n    takeLock.lock();\n}\n\n\/\/ \u628a\u56fa\u5b9a\u7684\u91ca\u653e\u9501\u987a\u5e8f\u5c01\u88c5\u5728\u65b9\u6cd5\u5185\uff0c\u786e\u4fdd\u6240\u6709\u7684\u5bf9\u4e24\u628a\u9501\u7684\u91ca\u653e\u987a\u5e8f\u90fd\u662f\u4e00\u81f4\u7684\u3002\nvoid fullyUnlock() {\n    takeLock.unlock();\n    putLock.unlock();\n}\n<\/code><\/pre>\n<h4>put \u64cd\u4f5c<\/h4>\n<p><code>put<\/code> \u64cd\u4f5c\u628a\u6307\u5b9a\u5143\u7d20\u6dfb\u52a0\u5230\u961f\u5c3e\uff0c\u5982\u679c\u6ca1\u6709\u7a7a\u95f4\u5219\u4e00\u76f4\u7b49\u5f85\u3002<\/p>\n<pre><code class=\"java\">public void put(E e) throws InterruptedException {\n    if (e == null) throw new NullPointerException();\n\n    \/\/ \u5728\u6240\u6709\u7684 put\/take\/etc \u7b49\u64cd\u4f5c\u4e2d\u9884\u8bbe\u503c\u672c\u5730\u53d8\u91cf c \u4e3a\u8d1f\u6570\u8868\u793a\u5931\u8d25\u3002\u6210\u529f\u4f1a\u8bbe\u7f6e\u4e3a &gt;= 0 \u7684\u503c\u3002\n    int c = -1;\n    Node&lt;E&gt; node = new Node(e);\n\n    \/\/ \u4e0b\u9762\u4e24\u884c\u662f\u8bbf\u95ee\u4f18\u5316\n    final ReentrantLock putLock = this.putLock;\n    final AtomicInteger count = this.count;\n\n    putLock.lockInterruptibly();\n    try {\n        \/*\n         * \u6ce8\u610f\uff0ccount\u7528\u4e8e\u7b49\u5f85\u76d1\u89c6\uff0c\u5373\u4f7f\u5b83\u6ca1\u6709\u7528\u9501\u4fdd\u62a4\u3002\u8fd9\u4e2a\u53ef\u884c\u662f\u56e0\u4e3a\n         * count \u53ea\u80fd\u5728\u6b64\u523b\uff08\u6301\u6709putLock\uff09\u51cf\u5c0f\uff08\u5176\u4ed6put\u7ebf\u7a0b\u90fd\u88ab\u9501\u62d2\u4e4b\u95e8\u5916\uff09\uff0c\n         * \u5f53count\u5bf9capacity\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5f53\u524d\u7ebf\u7a0b\uff08\u6216\u5176\u4ed6put\u7b49\u5f85\u7ebf\u7a0b\uff09\u5c06\u88ab\u901a\u77e5\u3002\n         * \u5728\u5176\u4ed6\u7b49\u5f85\u76d1\u89c6\u7684\u4f7f\u7528\u4e2d\u4e5f\u7c7b\u4f3c\u3002\n         *\/\n        while (count.get() == capacity) {\n            notFull.await();\n        }\n\n        enqueue(node);\n        c = count.getAndIncrement();\n\n        \/\/ \u8fd8\u6709\u53ef\u6dfb\u52a0\u7a7a\u95f4\u5219\u5524\u9192put\u7b49\u5f85\u7ebf\u7a0b\u3002\n        if (c + 1 &lt; capacity)\n            notFull.signal();\n    } finally {\n        putLock.unlock();\n    }\n\n    \/\/ c \u7531 count.getAndIncrement()\u8fd4\u56de\uff0c\u5982\u679c\u7b49\u4e8e0\uff0c\n    \/\/ \u5219 count \u5e94\u8be5\u662f\u5927\u4e8e\u7b49\u4e8e 1 \u4e86\uff0c\u5524\u9192take\u7ebf\u7a0b\u3002\n    if (c == 0)\n        signalNotEmpty();\n}\n<\/code><\/pre>\n<h4>take \u64cd\u4f5c<\/h4>\n<p><code>take<\/code> \u64cd\u4f5c\u4f1a\u4e00\u76f4\u963b\u585e\u76f4\u5230\u6709\u5143\u7d20\u53ef\u8fd4\u56de\u3002<\/p>\n<pre><code class=\"java\">public E take() throws InterruptedException {\n    E x;\n    int c = -1;\n    \/\/ \u4e0b\u9762\u4e24\u884c\u662f\u8bbf\u95ee\u4f18\u5316\n    final AtomicInteger count = this.count;\n    final ReentrantLock takeLock = this.takeLock;\n\n    takeLock.lockInterruptibly();\n    try {\n      \/\/ \u5faa\u73af\u91cc\u7b49\u5f85\u76f4\u5230\u6709\u6570\u636e\u53ef\u83b7\u53d6\n        while (count.get() == 0) {\n            notEmpty.await();\n        }\n\n        \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u6709\u6548\u5143\u7d20\n        x = dequeue();\n\n        \/\/ \u5982\u679c\u8fd8\u6709\u53ef\u83b7\u53d6\u5143\u7d20\uff0c\u5524\u9192\u7b49\u5f85\u83b7\u53d6\u7684\u7ebf\u7a0b\u3002\n        c = count.getAndDecrement();\n        if (c &gt; 1)\n            notEmpty.signal();\n    } finally {\n        takeLock.unlock();\n    }\n\n    \/\/ \u6ce8\u610f\uff0cc \u662f\u8c03\u7528 getAndDecrement \u8fd4\u56de\u7684\uff0c\u5982\u679c if \u6210\u7acb\uff0c\n    \/\/ \u8868\u660e\u5f53\u524d\u7684 count \u662f capacity - 1\uff0c\u53ef\u4ee5\u6dfb\u52a0\u65b0\u5143\u7d20\uff0c\u6240\u4ee5\u5524\u9192 \u6dfb\u52a0\u7ebf\u7a0b\u3002\n    if (c == capacity)\n        signalNotFull();\n    return x;\n}\n<\/code><\/pre>\n<h4>remove \u64cd\u4f5c<\/h4>\n<pre><code class=\"java\">\/\/ \u79fb\u9664\u6307\u5b9a\u5143\u7d20\u3002\u7531\u4e8e\u79fb\u9664\u5143\u7d20\u6d89\u53ca\u8be5\u7ed3\u70b9\u524d\u540e\u4e24\u4e2a\u7ed3\u70b9\u7684\u8bbf\u95ee\u4e0e\u4fee\u6539\uff0c\n\/\/ \u5bf9\u4e24\u628a\u9501\u52a0\u9501\u7b80\u5316\u4e86\u540c\u6b65\u7ba1\u7406\u3002\npublic boolean remove(Object o) {\n    if (o == null ) return false;\n\n    fullyLock();\n    try {\n        for (Node&lt;E&gt; trail = head, p = trail.next;\n             p != null ;\n             trail = p, p = p.next) {\n            if (o.equals(p.item)) {\n                unlink(p, trail);\n                return true ;\n            }\n        }\n        return false ;\n    } finally {\n        fullyUnlock();\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.LinkedBlockingQueue &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131226\/685\">\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":[110,144],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/685"}],"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=685"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/685\/revisions"}],"predecessor-version":[{"id":686,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/685\/revisions\/686"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}