{"id":392,"date":"2016-04-07T18:21:03","date_gmt":"2016-04-07T10:21:03","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=392"},"modified":"2016-09-04T17:58:28","modified_gmt":"2016-09-04T09:58:28","slug":"%e4%bb%8equeue%e6%8e%a5%e5%8f%a3%e8%af%b4%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=392","title":{"rendered":"\u4eceQueue\u63a5\u53e3\u8bf4\u961f\u5217"},"content":{"rendered":"<p>\n\tQueue\u662f\u961f\u5217\u7684\u610f\u601d\u3002\u961f\u5217\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u6027\u8868\uff0c\u7279\u6b8a\u4e4b\u5904\u5728\u4e8e\u5b83\u53ea\u5141\u8bb8\u5728\u8868\u7684\u524d\u7aef\uff08front\uff09\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\uff0c\u800c\u5728\u8868\u7684\u540e\u7aef\uff08rear\uff09\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u548c\u6808\u4e00\u6837\uff0c\u961f\u5217\u662f\u4e00\u79cd\u64cd\u4f5c\u53d7\u9650\u5236\u7684\u7ebf\u6027\u8868\u3002\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\u7684\u7aef\u79f0\u4e3a\u961f\u5c3e\uff0c\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u7684\u7aef\u79f0\u4e3a\u961f\u5934\u3002\u961f\u5217\u4e2d\u6ca1\u6709\u5143\u7d20\u65f6\uff0c\u79f0\u4e3a\u7a7a\u961f\u5217\u3002<br \/>\n\t\u961f\u5217\u7279\u6027\uff1a<strong>\u5148\u8fdb\u5148\u51fa\uff08FIFO)&mdash;&mdash;\u5148\u8fdb\u961f\u5217\u7684\u5143\u7d20\u5148\u51fa\u961f\u5217<\/strong>\u3002\u6765\u6e90\u4e8e\u6211\u4eec\u751f\u6d3b\u4e2d\u7684\u961f\u5217\uff08\u5148\u6392\u961f\u7684\u5148\u529e\u5b8c\u4e8b\uff09\u3002<br \/>\n\tQueue\u63a5\u53e3\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic interface Queue&lt;E&gt; extends Collection&lt;E&gt; {\r\n&nbsp; &nbsp; boolean add(E e);\r\n&nbsp; &nbsp; boolean offer(E e);\r\n&nbsp; &nbsp; E remove();\r\n&nbsp; &nbsp; E poll();\r\n&nbsp; &nbsp; E element();\r\n&nbsp; &nbsp; E peek();\r\n}<\/pre>\n<p>\n\t\u63a5\u53e3\u4f7f\u7528\u89e3\u91ca\uff1a(\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u6709\u4e24\u79cd\u5f62\u5f0f)\n<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:600px\">\n<tbody>\n<tr>\n<td>\n\t\t\t\t\u64cd\u4f5c\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u629b\u51fa\u5f02\u5e38\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u8fd4\u56de\u7279\u6b8a\u503c\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t\u63d2\u5165\n\t\t\t<\/td>\n<td>\n\t\t\t\tadd(e)\n\t\t\t<\/td>\n<td>\n\t\t\t\toffer(e)\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t\u5220\u9664\n\t\t\t<\/td>\n<td>\n\t\t\t\tremove()\n\t\t\t<\/td>\n<td>\n\t\t\t\tpoll()\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t\u68c0\u67e5\n\t\t\t<\/td>\n<td>\n\t\t\t\telement()\n\t\t\t<\/td>\n<td>\n\t\t\t\tpeek()\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\t&nbsp; &nbsp;&nbsp;<br \/>\n\tBlockingQueue\u961f\u5217\u63a5\u53e3\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic interface BlockingQueue&lt;E&gt; extends Queue&lt;E&gt; {\r\n&nbsp; &nbsp; boolean add(E e);\r\n&nbsp; &nbsp; boolean offer(E e);\r\n&nbsp; &nbsp; void put(E e) throws InterruptedException;\r\n&nbsp; &nbsp; boolean offer(E e, long timeout, TimeUnit unit)\r\n&nbsp; &nbsp; &nbsp; &nbsp; throws InterruptedException;\r\n&nbsp; &nbsp; E take() throws InterruptedException;\r\n&nbsp; &nbsp; E poll(long timeout, TimeUnit unit)\r\n&nbsp; &nbsp; &nbsp; &nbsp; throws InterruptedException;\r\n&nbsp; &nbsp; int remainingCapacity();\r\n&nbsp; &nbsp; boolean remove(Object o);\r\n&nbsp; &nbsp; public boolean contains(Object o);\r\n&nbsp; &nbsp; int drainTo(Collection&lt;? super E&gt; c);\r\n&nbsp; &nbsp; int drainTo(Collection&lt;? super E&gt; c, int maxElements);\r\n}<\/pre>\n<p>\n\t\u63a5\u53e3\u4f7f\u7528\u89e3\u91ca\uff1a\n<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:700px\">\n<thead>\n<tr>\n<th scope=\"col\">\n\t\t\t\t\u64cd\u4f5c\n\t\t\t<\/th>\n<th scope=\"col\">\n\t\t\t\t\u629b\u51fa\u5f02\u5e38\n\t\t\t<\/th>\n<th scope=\"col\">\n\t\t\t\t\u7279\u6b8a\u503c\n\t\t\t<\/th>\n<th scope=\"col\">\n\t\t\t\t\u963b\u585e\n\t\t\t<\/th>\n<th scope=\"col\">\n\t\t\t\t\u8d85\u65f6\n\t\t\t<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n\t\t\t\t\u63d2\u5165\n\t\t\t<\/td>\n<td>\n\t\t\t\tadd(e)\n\t\t\t<\/td>\n<td>\n\t\t\t\toffer(e)\n\t\t\t<\/td>\n<td>\n\t\t\t\tput(e)\n\t\t\t<\/td>\n<td>\n\t\t\t\toffer(e, time, unit)\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t\u79fb\u9664\n\t\t\t<\/td>\n<td>\n\t\t\t\tremove()\n\t\t\t<\/td>\n<td>\n\t\t\t\tpoll()\n\t\t\t<\/td>\n<td>\n\t\t\t\ttake()\n\t\t\t<\/td>\n<td>\n\t\t\t\tpoll(time, unit)\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t\u68c0\u67e5\n\t\t\t<\/td>\n<td>\n\t\t\t\telement()\n\t\t\t<\/td>\n<td>\n\t\t\t\tpeek()\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u4e0d\u53ef\u7528\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u4e0d\u53ef\u7528\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\n<\/p>\n<p>\n\t<span style=\"color:#FF0000\"><strong>1)add(anObject):<\/strong><\/span>\u628aanObject\u52a0\u5230BlockingQueue\u91cc,\u5373\u5982\u679cBlockingQueue\u53ef\u4ee5\u5bb9\u7eb3,\u5219\u8fd4\u56detrue,\u5426\u5219\u62a5\u5f02\u5e38&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>2)offer(anObject):<\/strong><\/span>\u8868\u793a\u5982\u679c\u53ef\u80fd\u7684\u8bdd,\u5c06anObject\u52a0\u5230BlockingQueue\u91cc,\u5373\u5982\u679cBlockingQueue\u53ef\u4ee5\u5bb9\u7eb3,\u5219\u8fd4\u56detrue,\u5426\u5219\u8fd4\u56defalse.&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>3)put(anObject):<\/strong><\/span>\u628aanObject\u52a0\u5230BlockingQueue\u91cc,\u5982\u679cBlockQueue\u6ca1\u6709\u7a7a\u95f4,\u5219\u8c03\u7528\u6b64\u65b9\u6cd5\u7684\u7ebf\u7a0b\u88ab\u963b\u65ad\u76f4\u5230BlockingQueue\u91cc\u9762\u6709\u7a7a\u95f4\u518d\u7ee7\u7eed.&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>4)poll(time):<\/strong><\/span>\u53d6\u8d70BlockingQueue\u91cc\u6392\u5728\u9996\u4f4d\u7684\u5bf9\u8c61,\u82e5\u4e0d\u80fd\u7acb\u5373\u53d6\u51fa,\u5219\u53ef\u4ee5\u7b49time\u53c2\u6570\u89c4\u5b9a\u7684\u65f6\u95f4,\u53d6\u4e0d\u5230\u65f6\u8fd4\u56denull&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>5)take():<\/strong><\/span>\u53d6\u8d70BlockingQueue\u91cc\u6392\u5728\u9996\u4f4d\u7684\u5bf9\u8c61,\u82e5BlockingQueue\u4e3a\u7a7a,\u963b\u65ad\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u76f4\u5230Blocking\u6709\u65b0\u7684\u5bf9\u8c61\u88ab\u52a0\u5165\u4e3a\u6b62&nbsp;\n<\/p>\n<p>\n\t<strong>\u963b\u585e\u961f\u5217\u7684\u7279\u6b8a\u6027\u662f\uff1a\u5982\u679cBlockQueue\u662f\u7a7a\u7684,\u4eceBlockingQueue\u53d6\u4e1c\u897f\u7684\u64cd\u4f5c\u5c06\u4f1a\u88ab\u963b\u65ad\u8fdb\u5165\u7b49\u5f85\u72b6\u6001,\u76f4\u5230BlockingQueue\u8fdb\u4e86\u4e1c\u897f\u624d\u4f1a\u88ab\u5524\u9192.\u540c\u6837,\u5982\u679cBlockingQueue\u662f\u6ee1\u7684,\u4efb\u4f55\u8bd5\u56fe\u5f80\u91cc\u5b58\u4e1c\u897f\u7684\u64cd\u4f5c\u4e5f\u4f1a\u88ab\u963b\u65ad\u8fdb\u5165\u7b49\u5f85\u72b6\u6001,\u76f4\u5230BlockingQueue\u91cc\u6709\u7a7a\u95f4\u624d\u4f1a\u88ab\u5524\u9192\u7ee7\u7eed\u64cd\u4f5c<\/strong>\n<\/p>\n<p>\n\tBlockingQueue\u6709\u56db\u4e2a\u5177\u4f53\u7684\u5b9e\u73b0\u7c7b,\u6839\u636e\u4e0d\u540c\u9700\u6c42,\u9009\u62e9\u4e0d\u540c\u7684\u5b9e\u73b0\u7c7b&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>1)ArrayBlockingQueue:<\/strong><\/span>\u89c4\u5b9a\u5927\u5c0f\u7684BlockingQueue,\u5176\u6784\u9020\u51fd\u6570\u5fc5\u987b\u5e26\u4e00\u4e2aint\u53c2\u6570\u6765\u6307\u660e\u5176\u5927\u5c0f.\u5176\u6240\u542b\u7684\u5bf9\u8c61\u662f\u4ee5FIFO(\u5148\u5165\u5148\u51fa)\u987a\u5e8f\u6392\u5e8f\u7684.&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>2)LinkedBlockingQueue:<\/strong><\/span>\u5927\u5c0f\u4e0d\u5b9a\u7684BlockingQueue,\u82e5\u5176\u6784\u9020\u51fd\u6570\u5e26\u4e00\u4e2a\u89c4\u5b9a\u5927\u5c0f\u7684\u53c2\u6570,\u751f\u6210\u7684BlockingQueue\u6709\u5927\u5c0f\u9650\u5236,\u82e5\u4e0d\u5e26\u5927\u5c0f\u53c2\u6570,\u6240\u751f\u6210\u7684BlockingQueue\u7684\u5927\u5c0f\u7531Integer.MAX_VALUE\u6765\u51b3\u5b9a.\u5176\u6240\u542b\u7684\u5bf9\u8c61\u662f\u4ee5FIFO(\u5148\u5165\u5148\u51fa)\u987a\u5e8f\u6392\u5e8f\u7684&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>3)PriorityBlockingQueue:<\/strong><\/span>\u7c7b\u4f3c\u4e8eLinkedBlockQueue,\u4f46\u5176\u6240\u542b\u5bf9\u8c61\u7684\u6392\u5e8f\u4e0d\u662fFIFO,\u800c\u662f\u4f9d\u636e\u5bf9\u8c61\u7684\u81ea\u7136\u6392\u5e8f\u987a\u5e8f\u6216\u8005\u662f\u6784\u9020\u51fd\u6570\u7684Comparator\u51b3\u5b9a\u7684\u987a\u5e8f.&nbsp;<br \/>\n\t<span style=\"color:#FF0000\"><strong>4)SynchronousQueue:<\/strong><\/span>\u7279\u6b8a\u7684BlockingQueue,\u5bf9\u5176\u7684\u64cd\u4f5c\u5fc5\u987b\u662f\u653e\u548c\u53d6\u4ea4\u66ff\u5b8c\u6210\u7684.&nbsp;<br \/>\n\t\u6ce8\u610f\uff1a<strong>LinkedBlockingQueue\u548cArrayBlockingQueue\u6bd4\u8f83\u8d77\u6765,\u5b83\u4eec\u80cc\u540e\u6240\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e0d\u4e00\u6837,\u5bfc\u81f4LinkedBlockingQueue\u7684\u6570\u636e\u541e\u5410\u91cf\u8981\u5927\u4e8eArrayBlockingQueue,\u4f46\u5728\u7ebf\u7a0b\u6570\u91cf\u5f88\u5927\u65f6\u5176\u6027\u80fd\u7684\u53ef\u9884\u89c1\u6027\u4f4e\u4e8eArrayBlockingQueue.<\/strong><br \/>\n\t\u603b\u7ed3\u4e00\u4e0b\u961f\u5217\u6240\u5177\u6709\u7684\u51e0\u4e2a\u64cd\u4f5c\uff1a<br \/>\n\t<strong>InitQueue\uff08\uff09\u3000\u3000&nbsp;&mdash;&mdash;\u521d\u59cb\u5316\u961f\u5217<br \/>\n\tEnQueue\uff08\uff09 &nbsp; &nbsp; &nbsp; &nbsp;&mdash;&mdash;\u8fdb\u961f\u5217<br \/>\n\tDeQueue\uff08\uff09 &nbsp; &nbsp; &nbsp; &nbsp;&mdash;&mdash;\u51fa\u961f\u5217<br \/>\n\tIsQueueEmpty\uff08\uff09&mdash;&mdash;\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a<br \/>\n\tIsQueueFull\uff08\uff09 &nbsp; &nbsp;&mdash;&mdash;\u5224\u65ad\u961f\u5217\u662f\u5426\u5df2\u6ee1<\/strong>\n<\/p>\n<p>\n\t&nbsp;<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=392\">\u4eceQueue\u63a5\u53e3\u8bf4\u961f\u5217<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Queue\u662f\u961f\u5217\u7684\u610f\u601d\u3002\u961f\u5217\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u6027\u8868\uff0c\u7279\u6b8a\u4e4b\u5904\u5728\u4e8e\u5b83\u53ea\u5141\u8bb8\u5728\u8868\u7684\u524d\u7aef\uff08front\uff09\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\uff0c\u800c\u5728 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[144,143,141,145,140,142],"class_list":["post-392","post","type-post","status-publish","format-standard","hentry","category-java","tag-arrayblockingqueue","tag-blockingqueue","tag-fifo","tag-linkedblockingqueue","tag-queue","tag-142"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=392"}],"version-history":[{"count":7,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions"}],"predecessor-version":[{"id":777,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions\/777"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}