{"id":1910,"date":"2023-04-01T09:18:15","date_gmt":"2023-04-01T01:18:15","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1910"},"modified":"2023-04-22T08:54:39","modified_gmt":"2023-04-22T00:54:39","slug":"element-unique-blocking-queue-linkedblockingqueue","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/element-unique-blocking-queue-linkedblockingqueue\/","title":{"rendered":"\u5143\u7d20\u552f\u4e00\u7684LinkedBlockingQueue\u963b\u585e\u961f\u5217"},"content":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u963b\u585e\u961f\u5217\uff0c\u5b83\u7ee7\u627f\u4e86JDK\u539f\u6709\u7684<code>LinkedBlockingQueue<\/code>\uff0c\u4e5f\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4e0e<code>LinkedBlockingQueue<\/code>\u4e0d\u540c\u7684\u5730\u65b9\u5728\u4e8e\uff0c<code>UniqueLinkedBlockingQueue<\/code>\u961f\u5217\u91cc\u9762\u4e0d\u5141\u8bb8\u51fa\u73b0\u91cd\u590d\u6027\u5143\u7d20\u3002\u8be5\u961f\u5217\u53ef\u4ee5\u5728\u5f88\u591a\u573a\u666f\u4e2d\u9002\u7528\uff0c\u6bd4\u5982\uff1a<\/p>\n<p>\u591a\u751f\u4ea7\u8005\u7684\u60c5\u5f62\u4e0b\uff0c\u4e00\u8d77\u5411\u961f\u5217\u4e2d\u5b58\u653e\u4efb\u52a1\uff0c\u8fd9\u4e9b\u4efb\u52a1\u4e0d\u5141\u8bb8\u5728\u961f\u5217\u91cc\u9762\u51fa\u73b0\u91cd\u590d\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u961f\u5217\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">import java.util.concurrent.LinkedBlockingQueue;\nimport java.util.concurrent.locks.ReentrantLock;\n\n\/**\n * \u5143\u7d20\u552f\u4e00\u7684LinkedBlockingQueue\u963b\u585e\u961f\u5217\n *\/\npublic class UniqueLinkedBlockingQueue&lt;E&gt; extends LinkedBlockingQueue&lt;E&gt; {\n\n    private static final long serialVersionUID = 6523805086129218113L;\n    private final ReentrantLock putLock = new ReentrantLock();\n\n    public void put(E e) throws InterruptedException {\n        putLock.lock();\n        try {\n            if (!contains(e)) {\n                super.put(e);\n            }\n        } finally {\n            putLock.unlock();\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u963b\u585e\u961f\u5217\uff0c\u5b83\u7ee7\u627f\u4e86JDK\u539f\u6709\u7684LinkedBlockingQueue\uff0c\u4e5f\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4e0eLink [&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-1910","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\/1910","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=1910"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1910\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}