{"id":1911,"date":"2023-04-01T09:18:58","date_gmt":"2023-04-01T01:18:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1911"},"modified":"2023-04-22T08:54:17","modified_gmt":"2023-04-22T00:54:17","slug":"blocking-queues-linkedblockingqueue","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/blocking-queues-linkedblockingqueue\/","title":{"rendered":"\u963b\u585e\u961f\u5217\u4e4bLinkedBlockingQueue"},"content":{"rendered":"<h2>\u6982\u8ff0<\/h2>\n<p><code>LinkedBlockingQueue<\/code>\u5185\u90e8\u7531<strong>\u5355\u94fe\u8868\u5b9e\u73b0<\/strong>\uff0c\u53ea\u80fd\u4ecehead\u53d6\u5143\u7d20\uff0c\u4ecetail\u6dfb\u52a0\u5143\u7d20\u3002\u6dfb\u52a0\u5143\u7d20\u548c\u83b7\u53d6\u5143\u7d20\u90fd\u6709\u72ec\u7acb\u7684\u9501\uff0c\u4e5f\u5c31\u662f\u8bf4<strong>LinkedBlockingQueue\u662f\u8bfb\u5199\u5206\u79bb\u7684<\/strong>\uff0c\u8bfb\u5199\u64cd\u4f5c\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u3002<code>LinkedBlockingQueue<\/code>\u91c7\u7528<strong>\u53ef\u91cd\u5165\u9501(ReentrantLock)<\/strong>\u6765\u4fdd\u8bc1\u5728\u5e76\u53d1\u60c5\u51b5\u4e0b\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u6784\u9020\u5668<\/h2>\n<p><code>LinkedBlockingQueue<\/code>\u4e00\u5171\u6709\u4e09\u4e2a\u6784\u9020\u5668\uff0c\u5206\u522b\u662f\u65e0\u53c2\u6784\u9020\u5668\u3001\u53ef\u4ee5\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u5668\u3001\u53ef\u4ee5\u7a7f\u5165\u4e00\u4e2a\u5bb9\u5668\u7684\u6784\u9020\u5668\u3002\u5982\u679c\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u65f6\u5019\u8c03\u7528\u7684\u662f\u65e0\u53c2\u6784\u9020\u5668\uff0c<code>LinkedBlockingQueue<\/code>\u7684\u9ed8\u8ba4\u5bb9\u91cf\u662f<code>Integer.MAX_VALUE<\/code>\uff0c\u8fd9\u6837\u505a\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u961f\u5217\u8fd8\u6ca1\u6709\u6ee1\uff0c\u4f46\u662f\u5185\u5b58\u5374\u5df2\u7ecf\u6ee1\u4e86\u7684\u60c5\u51b5\uff08\u5185\u5b58\u6ea2\u51fa\uff09\u3002<\/p>\n<pre><code class=\"language-java\">public LinkedBlockingQueue();    \/\/\u8bbe\u7f6e\u5bb9\u91cf\u4e3aInteger.MAX\n\npublic LinkedBlockingQueue(int capacity);    \/\/\u8bbe\u7f6e\u6307\u5b9a\u5bb9\u91cf\n\npublic LinkedBlockingQueue(Collection&lt;? extends E&gt; c);    \/\/\u7a7f\u5165\u4e00\u4e2a\u5bb9\u5668\uff0c\u5982\u679c\u8c03\u7528\u8be5\u6784\u9020\u5668\uff0c\u5bb9\u91cf\u9ed8\u8ba4\u4e5f\u662fInteger.MAX_VALUE<\/code><\/pre>\n<h2>LinkedBlockingQueue\u5e38\u7528\u64cd\u4f5c<\/h2>\n<h3>\u53d6\u6570\u636e<\/h3>\n<ul>\n<li><strong><code>take()<\/code><\/strong>\uff1a\u9996\u9009\u3002\u5f53\u961f\u5217\u4e3a\u7a7a\u65f6\u963b\u585e<\/li>\n<li><code>poll()<\/code>\uff1a\u5f39\u51fa\u961f\u9876\u5143\u7d20\uff0c\u961f\u5217\u4e3a\u7a7a\u65f6\uff0c\u8fd4\u56de\u7a7a<\/li>\n<li><code>peek()<\/code>\uff1a\u548cpoll\u7c7b\u4f3c\uff0c\u8fd4\u56de\u961f\u5217\u961f\u9876\u5143\u7d20\uff0c\u4f46\u9876\u5143\u7d20\u4e0d\u5f39\u51fa\u3002\u961f\u5217\u4e3a\u7a7a\u65f6\u8fd4\u56denull<\/li>\n<li><code>remove(Object o)<\/code>\uff1a\u79fb\u9664\u67d0\u4e2a\u5143\u7d20\uff0c\u961f\u5217\u4e3a\u7a7a\u65f6\u629b\u51fa\u5f02\u5e38\u3002\u6210\u529f\u79fb\u9664\u8fd4\u56detrue<\/li>\n<\/ul>\n<h3>\u6dfb\u52a0\u6570\u636e<\/h3>\n<ul>\n<li><strong><code>put()<\/code><\/strong>\uff1a\u9996\u9009\u3002\u961f\u6ee1\u662f\u963b\u585e<\/li>\n<li><code>offer()<\/code>\uff1a\u961f\u6ee1\u65f6\u8fd4\u56defalse<\/li>\n<\/ul>\n<h3>\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a<\/h3>\n<p><code>size()<\/code>\u65b9\u6cd5\u4f1a\u904d\u5386\u6574\u4e2a\u961f\u5217\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a<code>O(n)<\/code>\uff0c\u6240\u4ee5\u6700\u597d\u9009\u7528<code>isEmtpy()<\/code><\/p>\n<h3>put\u5143\u7d20\u539f\u7406<\/h3>\n<p>\u57fa\u672c\u8fc7\u7a0b\uff1a<\/p>\n<ol>\n<li>\u5224\u65ad\u5143\u7d20\u662f\u5426\u4e3anull\uff0c\u4e3anull\u629b\u51fa\u5f02\u5e38<\/li>\n<li>\u52a0\u9501(\u53ef\u4e2d\u65ad\u9501)<\/li>\n<li>\u5224\u65ad\u961f\u5217\u957f\u5ea6\u662f\u5426\u5230\u8fbe\u5bb9\u91cf\uff0c\u5982\u679c\u5230\u8fbe\u4e00\u76f4\u7b49\u5f85<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u961f\u6ee1\uff0cenqueue()\u5728\u961f\u5c3e\u52a0\u5165\u5143\u7d20<\/li>\n<li>\u961f\u5217\u957f\u5ea6\u52a01\uff0c\u6b64\u65f6\u5982\u679c\u961f\u5217\u8fd8\u6ca1\u6709\u6ee1\uff0c\u8c03\u7528signal\u5524\u9192\u5176\u4ed6\u963b\u585e\u961f\u5217<\/li>\n<\/ol>\n<pre><code class=\"language-java\">public void put() throws InterruptedException {\n    if (e == null) throw new NullPointerException();\n\n    int c = -1;\n    Node&lt;E&gt; node = new Node&lt;E&gt;(e);\n    final ReentrantLock putLock = this.putLock;\n    final AtomicInteger count = this.count;\n    putLock.lockInterruptibly();\n    try {\n        while (count.get() == capacity) {\n            notFull.await();\n        }\n        enqueue(node);\n        c = count.getAndIncrement();\n        if (c + 1 &lt; capacity)\n            notFull.signal();\n    } finally {\n        putLock.unlock();\n    }\n}<\/code><\/pre>\n<h3>take\u5143\u7d20\u539f\u7406<\/h3>\n<p>\u57fa\u672c\u8fc7\u7a0b\uff1a<\/p>\n<ol>\n<li>\u52a0\u9501(\u4f9d\u65e7\u662fReentrantLock)\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684\u9501\u548c\u5199\u5165\u662f\u4e0d\u540c\u7684\u4e24\u628a\u9501<\/li>\n<li>\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5c31\u4e00\u76f4\u7b49\u5f85<\/li>\n<li>\u901a\u8fc7dequeue\u65b9\u6cd5\u53d6\u5f97\u6570\u636e<\/li>\n<li>\u53d6\u8d70\u5143\u7d20\u540e\u961f\u5217\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\u5524\u9192\u5176\u4ed6\u7b49\u5f85\u4e2d\u7684\u961f\u5217<\/li>\n<\/ol>\n<pre><code class=\"language-java\">public 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}<\/code><\/pre>\n<p><code>enqueue()<\/code>\u548c<code>dequeue()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u90fd\u6bd4\u8f83\u7b80\u5355\uff0c\u65e0\u975e\u5c31\u662f\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u961f\u5c3e\uff0c\u4ece\u961f\u9876\u53d6\u8d70\u5143\u7d20<\/p>\n<h2>LinkedBlockingQueue\u4e0eLinkedBlockingDeque\u6bd4\u8f83<\/h2>\n<p><code>LinkedBlockingDeque<\/code>\u548c<code>LinkedBlockingQueue<\/code>\u7684\u76f8\u540c\u70b9\u5728\u4e8e\uff1a<\/p>\n<ol>\n<li>\u57fa\u4e8e\u94fe\u8868<\/li>\n<li>\u5bb9\u91cf\u53ef\u9009\uff0c\u4e0d\u8bbe\u7f6e\u7684\u8bdd\uff0c\u5c31\u662fInt\u7684\u6700\u5927\u503c<\/li>\n<\/ol>\n<p>\u548c<code>LinkedBlockingQueue<\/code>\u7684\u4e0d\u540c\u70b9\u5728\u4e8e\uff1a<\/p>\n<ol>\n<li>\u53cc\u7aef\u94fe\u8868\u548c\u5355\u94fe\u8868<\/li>\n<li>\u4e0d\u5b58\u5728\u54e8\u5175\u8282\u70b9<\/li>\n<li>\u4e00\u628a\u9501+\u4e24\u4e2a\u6761\u4ef6<\/li>\n<\/ol>\n<h2>\u5b9e\u4f8b<\/h2>\n<pre><code class=\"language-java\">public class ProducerAndConsumer {\n    public static void main(String[] args) {\n        try {\n            BlockingQueue queue = new LinkedBlockingQueue(5);\n\n            ExecutorService executor = Executors.newFixedThreadPool(5);\n            Produer producer = new Produer(queue);\n            for (int i = 0; i &lt; 3; i++) {\n                executor.execute(producer);\n            }\n            executor.execute(new Consumer(queue));\n\n            executor.shutdown();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n\nclass Produer implements  Runnable {\n\n    private BlockingQueue queue;\n    private int nums = 20;  \/\/\u5faa\u73af\u6b21\u6570\n\n    \/\/\u6807\u8bb0\u6570\u636e\u7f16\u53f7\n    private static volatile AtomicInteger count = new AtomicInteger();\n    private boolean isRunning = true;\n    public Produer() {}\n\n    public Produer(BlockingQueue queue){\n        this.queue = queue;\n    }\n\n    public void run() {\n        String data = null;\n        try {\n            System.out.println(&quot;\u5f00\u59cb\u751f\u4ea7\u6570\u636e&quot;);\n            System.out.println(&quot;-----------------------&quot;);\n\n            while (nums &gt; 0) {\n                nums--;\n                count.decrementAndGet();\n\n                Thread.sleep(500);\n                System.out.println(Thread.currentThread().getId() + &quot;: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e&quot;);\n                queue.put(count.getAndIncrement());\n            }\n        } catch(Exception e) {\n            e.printStackTrace();\n            Thread.currentThread().interrupt();\n        } finally {\n            System.out.println(&quot;\u751f\u4ea7\u8005\u7ebf\u7a0b\u9000\u51fa\uff01&quot;);\n        }\n    }\n}\n\nclass Consumer implements Runnable {\n\n    private BlockingQueue queue;\n    private int nums = 20;\n    private boolean isRunning = true;\n\n    public Consumer() {}\n\n    public Consumer(BlockingQueue queue) {\n        this.queue = queue;\n    }\n\n    public void run() {\n        System.out.println(&quot;\u6d88\u8d39\u8005\u5f00\u59cb\u6d88\u8d39&quot;);\n        System.out.println(&quot;-------------------------&quot;);\n\n        while (nums &gt; 0) {\n            nums--;\n            try {\n                while (isRunning) {\n                    int data = (Integer) queue.take();\n                    Thread.sleep(500);\n                    System.out.println(&quot;\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f&quot; + data);\n            }\n\n            } catch(Exception e) {\n                e.printStackTrace();\n                Thread.currentThread().interrupt();\n            } finally {\n                System.out.println(&quot;\u6d88\u8d39\u8005\u7ebf\u7a0b\u9000\u51fa!&quot;);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u6548\u679c\uff1a<\/p>\n<pre><code>\u5f00\u59cb\u751f\u4ea7\u6570\u636e\n-----------------------\n\u5f00\u59cb\u751f\u4ea7\u6570\u636e\n-----------------------\n\u5f00\u59cb\u751f\u4ea7\u6570\u636e\n-----------------------\n\u6d88\u8d39\u8005\u5f00\u59cb\u6d88\u8d39\n-------------------------\n12: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n11: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n13: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n12: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n11: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n13: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n12: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n11: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n13: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n11: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n12: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n13: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n11: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n12: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n13: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n11: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n12: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -3\n13: \u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -2\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -2\n11 :\u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -2\n12 :\u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\n\u751f\u4ea7\u8005\u7ebf\u7a0b\u9000\u51fa\uff01\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -2\n\u751f\u4ea7\u8005\u7ebf\u7a0b\u9000\u51fa\uff01\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -1\n\u751f\u4ea7\u8005\u7ebf\u7a0b\u9000\u51fa\uff01\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -1\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -2\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -1\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -1\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -1\n\u6d88\u8d39\u8005\u6d88\u8d39\u7684\u6570\u636e\u662f: -1<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 LinkedBlockingQueue\u5185\u90e8\u7531\u5355\u94fe\u8868\u5b9e\u73b0\uff0c\u53ea\u80fd\u4ecehead\u53d6\u5143\u7d20\uff0c\u4ecetail\u6dfb\u52a0\u5143\u7d20\u3002\u6dfb\u52a0\u5143 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[442,443],"class_list":["post-1911","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-queue","tag-443"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1911"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1911\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}