{"id":3025,"date":"2026-09-14T11:04:22","date_gmt":"2026-09-14T03:04:22","guid":{"rendered":"http:\/\/139.196.114.170\/?p=3025"},"modified":"2026-09-14T11:04:22","modified_gmt":"2026-09-14T03:04:22","slug":"arrayblockingqueue","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=3025","title":{"rendered":"ArrayBlockingQueue"},"content":{"rendered":"<p><code>ArrayBlockingQueue<\/code> \u662f Java \u5e76\u53d1\u5305\u4e2d\u4e00\u4e2a\u57fa\u4e8e<strong>\u6570\u7ec4<\/strong>\u5b9e\u73b0\u7684<strong>\u6709\u754c\u963b\u585e\u961f\u5217<\/strong>\uff0c\u5176\u6838\u5fc3\u8bbe\u8ba1\u56f4\u7ed5 <code>ReentrantLock<\/code> \u548c\u4e24\u4e2a <code>Condition<\/code> \u6761\u4ef6\u53d8\u91cf\u5c55\u5f00\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u3002<\/p>\n<h3>\ud83d\udcca \u5e95\u5c42\u6570\u636e\u7ed3\u6784<\/h3>\n<p><code>ArrayBlockingQueue<\/code> \u7684\u6838\u5fc3\u5b57\u6bb5\u5b9a\u4e49\u6e05\u6670\u4f53\u73b0\u4e86\u5176\u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<ul>\n<li><strong><code>final Object[] items<\/code><\/strong>\uff1a\u4e00\u4e2a\u5b9a\u957f\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u3002\u5b83\u5728\u6784\u9020\u65f6\u521d\u59cb\u5316\uff0c\u5bb9\u91cf\u4e00\u65e6\u786e\u5b9a\u4fbf\u4e0d\u53ef\u66f4\u6539\u3002<\/li>\n<li><strong><code>int takeIndex<\/code><\/strong>\uff1a\u6307\u5411\u4e0b\u4e00\u4e2a\u88ab\u53d6\u51fa\u5143\u7d20\uff08\u961f\u5934\uff09\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong><code>int putIndex<\/code><\/strong>\uff1a\u6307\u5411\u4e0b\u4e00\u4e2a\u88ab\u63d2\u5165\u5143\u7d20\uff08\u961f\u5c3e\uff09\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong><code>int count<\/code><\/strong>\uff1a\u5f53\u524d\u961f\u5217\u4e2d\u7684\u5143\u7d20\u6570\u91cf\uff0c\u7528\u4e8e\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\u6216\u5df2\u6ee1\u3002<\/li>\n<li><strong><code>final ReentrantLock lock<\/code><\/strong>\uff1a\u4e00\u4e2a\u53ef\u91cd\u5165\u9501\uff0c\u7528\u4e8e\u63a7\u5236\u6240\u6709\u5e76\u53d1\u8bbf\u95ee\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u57fa\u7840\u3002<\/li>\n<li><strong><code>private final Condition notEmpty<\/code><\/strong>\uff1a\u6761\u4ef6\u53d8\u91cf\uff0c\u7528\u4e8e<strong>\u6d88\u8d39\u8005\u7ebf\u7a0b<\/strong>\u5728\u961f\u5217\u4e3a\u7a7a\u65f6\u7b49\u5f85\u3002<\/li>\n<li><strong><code>private final Condition notFull<\/code><\/strong>\uff1a\u6761\u4ef6\u53d8\u91cf\uff0c\u7528\u4e8e<strong>\u751f\u4ea7\u8005\u7ebf\u7a0b<\/strong>\u5728\u961f\u5217\u5df2\u6ee1\u65f6\u7b49\u5f85\u3002<\/li>\n<\/ul>\n<p>\u8be5\u961f\u5217\u5728\u903b\u8f91\u4e0a\u662f\u4e00\u4e2a<strong>\u5faa\u73af\u6570\u7ec4<\/strong>\u3002\u5f53 <code>putIndex<\/code> \u6216 <code>takeIndex<\/code> \u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u65f6\uff0c\u4f1a\u901a\u8fc7\u53d6\u6a21\u8fd0\u7b97\u56de\u5230\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e\uff080\uff09\uff0c\u4ece\u800c\u9ad8\u6548\u5730\u5229\u7528\u6570\u7ec4\u7a7a\u95f4\uff0c\u5b9e\u73b0\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u987a\u5e8f\u3002<\/p>\n<h3>\u2699\ufe0f \u6838\u5fc3\u65b9\u6cd5\u89e3\u6790<\/h3>\n<p><code>ArrayBlockingQueue<\/code> \u7684\u6838\u5fc3\u65b9\u6cd5\u90fd\u4f9d\u8d56\u4e8e <code>lock<\/code> \u548c <code>Condition<\/code> \u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u4e0e\u963b\u585e\u3002<\/p>\n<h4>1. \u5165\u961f\u65b9\u6cd5\uff1a<code>put(E e)<\/code><\/h4>\n<p><code>put<\/code> \u65b9\u6cd5\u7528\u4e8e\u5411\u961f\u5217\u5c3e\u90e8\u63d2\u5165\u5143\u7d20\u3002\u5982\u679c\u961f\u5217\u5df2\u6ee1\uff0c\u5b83\u4f1a<strong>\u963b\u585e<\/strong>\u5f53\u524d\u751f\u4ea7\u8005\u7ebf\u7a0b\uff0c\u76f4\u5230\u961f\u5217\u6709\u7a7a\u95f2\u7a7a\u95f4\u3002<\/p>\n<pre><code class=\"language-java\">public void put(E e) throws InterruptedException {\n    Objects.requireNonNull(e);\n    final ReentrantLock lock = this.lock;\n    lock.lockInterruptibly(); \/\/ \u83b7\u53d6\u53ef\u4e2d\u65ad\u9501\n    try {\n        while (count == items.length) \/\/ \u5982\u679c\u961f\u5217\u5df2\u6ee1\n            notFull.await(); \/\/ \u5728 notFull \u6761\u4ef6\u4e0a\u7b49\u5f85\n        enqueue(e); \/\/ \u5c06\u5143\u7d20\u63d2\u5165\u961f\u5217\n    } finally {\n        lock.unlock();\n    }\n}<\/code><\/pre>\n<p><strong>\u6d41\u7a0b\u89e3\u6790<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u9501<\/strong>\uff1a\u8c03\u7528 <code>lock.lockInterruptibly()<\/code> \u83b7\u53d6\u9501\uff0c\u6b64\u64cd\u4f5c\u53ef\u54cd\u5e94\u4e2d\u65ad\u3002<\/li>\n<li><strong>\u68c0\u67e5\u961f\u5217\u6ee1<\/strong>\uff1a\u5728 <code>while<\/code> \u5faa\u73af\u4e2d\u68c0\u67e5 <code>count == items.length<\/code>\u3002\u4f7f\u7528 <code>while<\/code> \u800c\u975e <code>if<\/code> \u662f\u4e3a\u4e86\u9632\u6b62<strong>\u865a\u5047\u5524\u9192<\/strong>\u3002<\/li>\n<li><strong>\u7b49\u5f85<\/strong>\uff1a\u82e5\u961f\u5217\u5df2\u6ee1\uff0c\u8c03\u7528 <code>notFull.await()<\/code>\uff0c\u7ebf\u7a0b\u88ab\u963b\u585e\u5e76\u91ca\u653e\u9501\uff0c\u8fdb\u5165 <code>notFull<\/code> \u7684\u7b49\u5f85\u961f\u5217\u3002<\/li>\n<li><strong>\u5165\u961f<\/strong>\uff1a\u5f53\u88ab\u5524\u9192\u4e14\u961f\u5217\u4e0d\u6ee1\u65f6\uff0c\u8c03\u7528 <code>enqueue(e)<\/code> \u6267\u884c\u5165\u961f\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u91ca\u653e\u9501<\/strong>\uff1a\u65e0\u8bba\u64cd\u4f5c\u6210\u529f\u4e0e\u5426\uff0c\u90fd\u5728 <code>finally<\/code> \u5757\u4e2d\u91ca\u653e\u9501\u3002<\/li>\n<\/ol>\n<h4>2. \u51fa\u961f\u65b9\u6cd5\uff1a<code>take()<\/code><\/h4>\n<p><code>take<\/code> \u65b9\u6cd5\u7528\u4e8e\u4ece\u961f\u5217\u5934\u90e8\u53d6\u51fa\u5e76\u79fb\u9664\u5143\u7d20\u3002\u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5b83\u4f1a<strong>\u963b\u585e<\/strong>\u5f53\u524d\u6d88\u8d39\u8005\u7ebf\u7a0b\uff0c\u76f4\u5230\u961f\u5217\u4e2d\u6709\u5143\u7d20\u53ef\u7528\u3002<\/p>\n<pre><code class=\"language-java\">public E take() throws InterruptedException {\n    final ReentrantLock lock = this.lock;\n    lock.lockInterruptibly();\n    try {\n        while (count == 0) \/\/ \u5982\u679c\u961f\u5217\u4e3a\u7a7a\n            notEmpty.await(); \/\/ \u5728 notEmpty \u6761\u4ef6\u4e0a\u7b49\u5f85\n        return dequeue(); \/\/ \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u5143\u7d20\u5e76\u8fd4\u56de\n    } finally {\n        lock.unlock();\n    }\n}<\/code><\/pre>\n<p><strong>\u6d41\u7a0b\u89e3\u6790<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u9501<\/strong>\uff1a\u540c\u6837\u901a\u8fc7 <code>lock.lockInterruptibly()<\/code> \u83b7\u53d6\u9501\u3002<\/li>\n<li><strong>\u68c0\u67e5\u961f\u5217\u7a7a<\/strong>\uff1a\u68c0\u67e5 <code>count == 0<\/code>\u3002<\/li>\n<li><strong>\u7b49\u5f85<\/strong>\uff1a\u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u8c03\u7528 <code>notEmpty.await()<\/code> \u963b\u585e\u7ebf\u7a0b\uff0c\u8fdb\u5165 <code>notEmpty<\/code> \u7684\u7b49\u5f85\u961f\u5217\u3002<\/li>\n<li><strong>\u51fa\u961f<\/strong>\uff1a\u5f53\u88ab\u5524\u9192\u4e14\u961f\u5217\u975e\u7a7a\u65f6\uff0c\u8c03\u7528 <code>dequeue()<\/code> \u6267\u884c\u51fa\u961f\u64cd\u4f5c\u5e76\u8fd4\u56de\u5143\u7d20\u3002<\/li>\n<li><strong>\u91ca\u653e\u9501<\/strong>\uff1a\u5728 <code>finally<\/code> \u5757\u4e2d\u91ca\u653e\u9501\u3002<\/li>\n<\/ol>\n<h4>3. \u975e\u963b\u585e\u65b9\u6cd5\uff1a<code>offer(E e)<\/code> \u4e0e <code>poll()<\/code><\/h4>\n<p>\u9664\u4e86\u963b\u585e\u65b9\u6cd5\uff0c\u8be5\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u975e\u963b\u585e\u7684\u66ff\u4ee3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong><code>offer(E e)<\/code><\/strong>\uff1a\u5c1d\u8bd5\u7acb\u5373\u63d2\u5165\u5143\u7d20\uff0c\u5982\u679c\u961f\u5217\u5df2\u6ee1\uff0c\u5219\u76f4\u63a5\u8fd4\u56de <code>false<\/code>\uff0c<strong>\u4e0d\u4f1a\u963b\u585e<\/strong>\u3002<\/li>\n<li><strong><code>poll()<\/code><\/strong>\uff1a\u5c1d\u8bd5\u7acb\u5373\u53d6\u51fa\u5143\u7d20\uff0c\u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u76f4\u63a5\u8fd4\u56de <code>null<\/code>\uff0c<strong>\u4e0d\u4f1a\u963b\u585e<\/strong>\u3002<\/li>\n<\/ul>\n<h3>\ud83c\udfaf \u6838\u5fc3\u7528\u9014\u4e0e\u573a\u666f<\/h3>\n<p><code>ArrayBlockingQueue<\/code> \u662f\u5b9e\u73b0<strong>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f<\/strong>\u7684\u7ecf\u5178\u5de5\u5177\uff0c\u5176\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u89e3\u8026\u3001\u7f13\u51b2\u548c\u6d41\u91cf\u63a7\u5236\u3002<\/p>\n<ul>\n<li><strong>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u534f\u4f5c<\/strong>\uff1a\u4f5c\u4e3a\u4e24\u8005\u4e4b\u95f4\u7684\u7f13\u51b2\u533a\uff0c\u81ea\u52a8\u8c03\u8282\u901f\u5ea6\u5dee\u5f02\u3002\u961f\u5217\u6ee1\u65f6\u751f\u4ea7\u8005\u963b\u585e\uff0c\u961f\u5217\u7a7a\u65f6\u6d88\u8d39\u8005\u963b\u585e\uff0c\u907f\u514d\u4e86\u5fd9\u7b49\u5f85\uff0c\u63d0\u5347\u4e86 CPU \u5229\u7528\u7387\u3002<\/li>\n<li><strong>\u6d41\u91cf\u63a7\u5236<\/strong>\uff1a\u7531\u4e8e\u662f\u6709\u754c\u961f\u5217\uff0c\u53ef\u4ee5\u7528\u6765\u9650\u5236\u7cfb\u7edf\u4e2d\u5f85\u5904\u7406\u4efb\u52a1\u7684\u6570\u91cf\uff0c\u9632\u6b62\u8d44\u6e90\u8fc7\u8f7d\uff0c\u5bf9\u5185\u5b58\u654f\u611f\u7684\u5e94\u7528\u573a\u666f\u5c24\u5176\u9002\u7528\u3002<\/li>\n<li><strong>\u4efb\u52a1\u8c03\u5ea6\u4e0e\u7ebf\u7a0b\u6c60<\/strong>\uff1a\u5e38\u7528\u4f5c\u7ebf\u7a0b\u6c60\uff08\u5982 <code>ThreadPoolExecutor<\/code>\uff09\u7684\u5de5\u4f5c\u961f\u5217\uff0c\u5b58\u50a8\u5f85\u6267\u884c\u7684\u4efb\u52a1\uff0c\u7531\u5de5\u4f5c\u7ebf\u7a0b\u6d88\u8d39\u3002<\/li>\n<li><strong>\u7cfb\u7edf\u89e3\u8026<\/strong>\uff1a\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u65e0\u9700\u77e5\u9053\u5bf9\u65b9\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u53ea\u9700\u4e0e\u961f\u5217\u4ea4\u4e92\uff0c\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/li>\n<\/ul>\n<h3>\u2696\ufe0f \u516c\u5e73\u6027\u7b56\u7565\u4e0e\u6027\u80fd\u8003\u91cf<\/h3>\n<ul>\n<li><strong>\u516c\u5e73\u6027\u9009\u62e9<\/strong>\uff1a\u6784\u9020\u65f6\u53ef\u6307\u5b9a <code>fair<\/code> \u53c2\u6570\u3002\u9ed8\u8ba4\u975e\u516c\u5e73\u7b56\u7565\u541e\u5410\u91cf\u66f4\u9ad8\uff1b\u516c\u5e73\u7b56\u7565\uff08<code>fair=true<\/code>\uff09\u5219\u4fdd\u8bc1\u7b49\u5f85\u7ebf\u7a0b\u6309 FIFO \u987a\u5e8f\u83b7\u53d6\u9501\uff0c\u51cf\u5c11\u7ebf\u7a0b\u9965\u997f\uff0c\u4f46\u4f1a\u964d\u4f4e\u541e\u5410\u91cf\u3002<\/li>\n<li><strong>\u6027\u80fd\u6743\u8861<\/strong>\uff1a\u4f7f\u7528<strong>\u5355\u9501<\/strong>\u8bbe\u8ba1\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u4e92\u65a5\uff0c\u9ad8\u5e76\u53d1\u4e0b\u53ef\u80fd\u6210\u4e3a\u74f6\u9888\u3002\u4f46\u5176\u5b58\u50a8\u7a7a\u95f4<strong>\u9884\u5148\u5206\u914d<\/strong>\uff0c\u4f7f\u7528\u65f6\u5185\u5b58\u5f00\u9500\u8f83\u5c0f\uff0c\u6027\u80fd\u66f4\u7a33\u5b9a\u3002<\/li>\n<li><strong>\u4e0e <code>LinkedBlockingQueue<\/code> \u7684\u5bf9\u6bd4<\/strong>\uff1a<code>ArrayBlockingQueue<\/code> \u5f3a\u5236\u6709\u754c\uff0c\u5185\u5b58\u9884\u5206\u914d\uff0c\u5355\u9501\u63a7\u5236\uff1b<code>LinkedBlockingQueue<\/code> \u9ed8\u8ba4\u65e0\u754c\uff08\u53ef\u6307\u5b9a\uff09\uff0c\u52a8\u6001\u5206\u914d\u8282\u70b9\uff0c\u901a\u5e38\u4f7f\u7528\u53cc\u9501\uff0c\u5728\u9ad8\u5e76\u53d1\u4e0b\u53ef\u80fd\u6709\u66f4\u597d\u7684\u541e\u5410\u91cf\uff0c\u4f46\u5185\u5b58\u5f00\u9500\u53ef\u80fd\u66f4\u5927\u4e14\u6027\u80fd\u6ce2\u52a8\u66f4\u660e\u663e\u3002<\/li>\n<\/ul>\n<p>\u603b\u7684\u6765\u8bf4\uff0c<code>ArrayBlockingQueue<\/code> \u662f\u4e00\u4e2a\u8bbe\u8ba1\u7cbe\u826f\u3001\u7528\u9014\u660e\u786e\u7684\u5e76\u53d1\u7ec4\u4ef6\u3002\u5b83\u7684\u6709\u754c\u7279\u6027\u548c\u963b\u585e\u673a\u5236\u4f7f\u5176\u6210\u4e3a\u8d44\u6e90\u53d7\u9650\u7684\u591a\u7ebf\u7a0b\u573a\u666f\u4e2d\u5b9e\u73b0\u7a33\u5b9a\u3001\u9ad8\u6548\u6570\u636e\u4ea4\u6362\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ArrayBlockingQueue \u662f Java \u5e76\u53d1\u5305\u4e2d\u4e00\u4e2a\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6709\u754c\u963b\u585e\u961f\u5217\uff0c\u5176\u6838\u5fc3\u8bbe\u8ba1\u56f4\u7ed5 R &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=3025\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cArrayBlockingQueue\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[69],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/3025"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3025"}],"version-history":[{"count":1,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/3025\/revisions"}],"predecessor-version":[{"id":3026,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/3025\/revisions\/3026"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3025"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}