
{"id":2228,"date":"2020-09-01T16:08:22","date_gmt":"2020-09-01T08:08:22","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2228"},"modified":"2020-09-04T08:53:56","modified_gmt":"2020-09-04T00:53:56","slug":"2020java%e6%95%99%e7%a8%8b%ef%bc%9aqueue%e9%98%9f%e5%88%97%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2228","title":{"rendered":"2020java\u6559\u7a0b\uff1aQueue\u961f\u5217(\u63a5\u53e3)"},"content":{"rendered":"<h3>Queue\u961f\u5217(\u63a5\u53e3)<\/h3>\n<p>Collection\u7684\u5b50\u63a5\u53e3\uff0c\u8868\u793a\u961f\u5217FIFO(\u5148\u8fdb\u5148\u51fa)<br \/>\n<strong>\u5e38\u7528\u65b9\u6cd5<\/strong>\uff1a<br \/>\n&#8211; \u629b\u51fa\u5f02\u5e38\uff1a<br \/>\n&#8212; boolean add((E e);\/\/\u987a\u5e8f\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20(\u5230\u8fbe\u4e0a\u9650\u540e\uff0c\u518d\u6dfb\u52a0\u5219\u4f1a\u629b\u5f02\u5e38)<br \/>\n&#8212; E remove()\/\/\u83b7\u5f97\u7b2c\u4e00\u4e2a\u5143\u7d20\u5e76\u79fb\u9664(\u5982\u679c\u961f\u5217\u6ca1\u6709\u5143\u7d20\u65f6\uff0c\u5219\u4f1a\u629b\u5f02\u5e38)<br \/>\n&#8212; E element()\/\/\u83b7\u5f97\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f46\u4e0d\u79fb\u9664(\u5982\u679c\u961f\u5217\u6ca1\u6709\u5143\u7d20\u65f6\uff0c\u5219\u4f1a\u629b\u5f02\u5e38)<br \/>\n&#8211; \u8fd4\u56de\u7279\u6b8a\u503c\uff1a<br \/>\n&#8212; boolean offer(E e)\/\/\u987a\u5e8f\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20(\u5230\u8fbe\u4e0a\u9650\u540e\uff0c\u518d\u6dfb\u52a0\u5219\u4f1a\u8fd4\u56defalse)<br \/>\n&#8212; E poll()\/\/\u83b7\u5f97\u7b2c\u4e00\u4e2a\u5143\u7d20\u5e76\u79fb\u9664(\u5982\u679c\u961f\u5217\u6ca1\u6709\u5143\u7d20\u65f6\uff0c\u5219\u8fd4\u56denull)<br \/>\n&#8212; E peek()\/\/\u83b7\u5f97\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f46\u4e0d\u79fb\u9664(\u5982\u679c\u961f\u5217\u6ca1\u6709\u5143\u7d20\u65f6\uff0c\u5219\u8fd4\u56denull)<!--more--><\/p>\n<p><strong>ConcurrentLinkedQueue<\/strong><br \/>\n\u7ebf\u7a0b\u5b89\u5168\uff0c\u53ef\u9ad8\u6548\u8bfb\u5199\u7684\u961f\u5217\uff0c\u9ad8\u5e76\u53d1\u4e0b\u6027\u80fd\u6700\u597d\u7684\u961f\u5217\u3002<br \/>\n\u65e0\u9501\u3001CAS\u6bd4\u8f83\u4ea4\u6362\u7b97\u6cd5\uff0c\u4fee\u6539\u7684\u65b9\u6cd5\u5305\u542b\u4e09\u4e2a\u6838\u5fc3\u53c2\u6570(V,E,N)<br \/>\nV\uff1a\u8981\u66f4\u65b0\u7684\u53d8\u91cf\u3002E\uff1a\u9884\u671f\u503c\u3002N\uff1a\u65b0\u503c<br \/>\n\u53ea\u6709\u5f53V==E\u65f6\uff0cV==N\uff1b\u5426\u5219\u8868\u793a\u5df2\u88ab\u66f4\u65b0\u8fc7\uff0c\u5219\u53d6\u6d88\u5f53\u524d\u64cd\u4f5c\u3002<\/p>\n<pre>Queue&lt;String&gt; q=new ConcurrentLinkedQueue&lt;&gt;();\r\nq.offer(\"Hello\");\r\nq.offer(\"World\");\r\nSystem.out.println(q.poll());\r\nSystem.out.println(q.peek());<\/pre>\n<p><strong>BlockingQueue\u63a5\u53e3(\u963b\u585e\u961f\u5217)<\/strong><br \/>\nQueue\u7684\u5b50\u63a5\u53e3\uff0c\u963b\u585e\u7684\u961f\u5217\uff0c\u589e\u52a0\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u72b6\u6001\u4e3a\u65e0\u9650\u671f\u7b49\u5f85\u7684\u65b9\u6cd5\u3002<br \/>\n\u65b9\u6cd5\uff1a<br \/>\nvoid put(E e)\/\/\u5c06\u6307\u5b9a\u5143\u7d20\u63d2\u5165\u6b64\u961f\u5217\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u53ef\u7528\u7a7a\u95f4\uff0c\u5219\u7b49\u5f85\u3002<br \/>\nE take()\/\/\u83b7\u53d6\u5e76\u79fb\u9664\u6b64\u961f\u5217\u5934\u90e8\u5143\u7d20\uff0c\u5982\u679c\u6ca1\u6709\u53ef\u7528\u5143\u7d20\uff0c\u5219\u7b49\u5f85\u3002<br \/>\n\u53ef\u7528\u4e8e\u89e3\u51b3\u751f\u4ea7\u8005\u3001\u6d88\u8d39\u4e4b\u95ee\u9898\u3002<\/p>\n<p>\u963b\u585e\u961f\u5217<br \/>\n<strong>ArrayBlockingQueue<\/strong>:<br \/>\n\u6570\u7ec4\u7ed3\u6784\u5b9e\u73b0\uff0c\u6709\u754c\u961f\u5217(\u624b\u5de5\u56fa\u5b9a\u4e0a\u9650)<\/p>\n<pre>Queue&lt;String&gt; q=new ArrayBlockingQueue&lt;String&gt;(10);\r\nq.offer(\"World\");\r\nSystem.out.println(q.peek());<\/pre>\n<p><strong>LinkedBlockingQueue<\/strong>\uff1a<br \/>\n\u94fe\u8868\u7ed3\u6784\u5b9e\u73b0\uff0c\u65e0\u754c\u961f\u5217(\u9ed8\u8ba4\u4e0a\u9650Intger.MAX_VALUE)<\/p>\n<pre>Queue&lt;String&gt; q=new LinkedBlockingQueue&lt;&gt;();\r\nq.offer(\"World\");\r\nSystem.out.println(q.peek());<\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>ExecutorService\u7ebf\u7a0b\u6c60\u63a5\u53e3\uff0cExecutors\u5de5\u5382\u3002<br \/>\nCallable\u7ebf\u7a0b\u4efb\u52a1\u3001Future\u5f02\u6b65\u8fd4\u56de\u503c\u3002<br \/>\nLock\u3001ReentrantLock\u91cd\u5165\u9501\uff0cReentrantReadWriteLock\u8bfb\u5199\u9501\u3002<br \/>\nCopyOnWriteArrayList\u7ebf\u7a0b\u5b89\u5168\u7684ArrayList<br \/>\nCopyOnWriteArraySet\u7ebf\u7a0b\u5b89\u5168\u7684ArraySet<br \/>\nConcurrentHashMap\u7ebf\u7a0b\u5b89\u5168\u7684HashMap<br \/>\nConcurrentLinkedQueue\u7ebf\u7a0b\u5b89\u5168\u7684Queue<br \/>\nArrayBlockingQueue\u7ebf\u7a0b\u5b89\u5168\u7684\u963b\u585eQueue(\u751f\u4ea7\u8005\uff0c\u6d88\u8d39\u8005)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Queue\u961f\u5217(\u63a5\u53e3) Collection\u7684\u5b50\u63a5\u53e3\uff0c\u8868\u793a\u961f\u5217FIFO(\u5148\u8fdb\u5148\u51fa) \u5e38\u7528\u65b9\u6cd5\uff1a &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2228","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2228"}],"version-history":[{"count":2,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":2244,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2228\/revisions\/2244"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}