{"id":64879,"date":"2025-05-03T17:33:22","date_gmt":"2025-05-03T09:33:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64879\/"},"modified":"2025-05-03T17:33:22","modified_gmt":"2025-05-03T09:33:22","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%a8%a1%e6%8b%9f%e5%85%ac%e5%b9%b3%e6%8a%a2%e7%a5%a8%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64879\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u6a21\u62df\u516c\u5e73\u62a2\u7968\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173086695125450.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u6a21\u62df\u516c\u5e73\u62a2\u7968\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u6a21\u62df\u516c\u5e73\u62a2\u7968\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u6a21\u62df\u516c\u5e73\u62a2\u7968<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u5982\u4f55\u6a21\u62df 100 \u4e2a\u4eba\u62a2 10 \u5f20\u7968\u7684\u95ee\u9898\uff0c\u9700\u8981\u786e\u4fdd\u516c\u5e73\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u4f9b\u53c2\u8003\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li> <strong>concurrenthashset<\/strong> \u548c <strong>atomicinteger<\/strong>\uff1aconcurrenthashset \u662f\u4e00\u4e2a\u5e76\u53d1\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\uff0c\u800c atomicinteger \u662f\u4e00\u4e2a\u539f\u5b50\u6027\u7684\u6574\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 concurrenthashset \u6765\u8bb0\u5f55\u5df2\u552e\u51fa\u7684\u7968\u53f7\uff0c\u5e76\u4f7f\u7528 atomicinteger \u6765\u8bb0\u5f55\u5269\u4f59\u7968\u6570\u3002\u5f53\u7ebf\u7a0b\u62a2\u7968\u65f6\uff0c\u5b83\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece concurrenthashset \u4e2d\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u7968\u53f7\uff0c\u5e76\u4e14\u5982\u679c\u7968\u53f7\u53ef\u7528\uff0c\u5219\u4f7f\u7528 atomicinteger \u9012\u51cf\u5269\u4f59\u7968\u6570\u3002<\/li>\n<li> <strong>\u516c\u5e73\u9501<\/strong>\uff1a\u516c\u5e73\u9501\u662f\u4e00\u79cd\u9501\uff0c\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u7ebf\u7a0b\u4ee5\u7533\u8bf7\u9501\u7684\u987a\u5e8f\u83b7\u53d6\u9501\u3002\u5728\u62a2\u7968\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reentrantlock \u548c\u5176 fr=true \u9009\u9879\u6765\u5b9e\u73b0\u516c\u5e73\u9501\u3002\u8fd9\u6837\uff0c\u7ebf\u7a0b\u5c06\u6309\u987a\u5e8f\u83b7\u53d6\u9501\uff0c\u4ee5\u786e\u4fdd\u516c\u5e73\u6027\u3002\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u516c\u5e73\u9501\u5b9e\u73b0\u62a2\u7968\uff1a<\/li>\n<\/ul>\n<pre>import java.util.concurrent.locks.ReentrantLock;\n\npublic class FairTicket\u6436\u7968 {\n    private static final int TOTAL_TICKETS = 10;\n    private static final int TOTAL_USERS = 100;\n\n    \/\/ \u4f7f\u7528\u516c\u5e73\u9501\n    private static ReentrantLock fairLock = new ReentrantLock(true);\n\n    public static void main(String[] args) {\n        for (int i = 0; i &lt; TOTAL_USERS; i++) {\n            new Thread(() -&gt; {\n                fairLock.lock();\n                try {\n                    if (TOTAL_TICKETS &gt; 0) {\n                        System.out.println(\"\u7528\u6237\" + Thread.currentThread().getName() + \"\u62a2\u5230\u4e86\u7b2c\" + TOTAL_TICKETS + \"\u5f20\u7968\");\n                        TOTAL_TICKETS--;\n                    } else {\n                        System.out.println(\"\u7528\u6237\" + Thread.currentThread().getName() + \"\u6ca1\u6709\u62a2\u5230\u7968\");\n                    }\n                } finally {\n                    fairLock.unlock();\n                }\n            }).start();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u6a21\u62df\u516c\u5e73\u62a2\u7968\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u6a21\u62df\u516c\u5e73\u62a2\u7968 \u4e3a\u4e86\u89e3\u51b3\u5982\u4f55\u6a21\u62df 100 \u4e2a\u4eba\u62a2 10 \u5f20\u7968\u7684\u95ee\u9898\uff0c\u9700\u8981\u786e\u4fdd\u516c\u5e73\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u4f9b\u53c2\u8003\u7684\u89e3\u51b3\u65b9\u6848\uff1a concurrenthashset \u548c atomicinteger\uff1aconcurrenthashset \u662f\u4e00\u4e2a\u5e76\u53d1\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\uff0c\u800c atomicinteger \u662f\u4e00\u4e2a\u539f\u5b50\u6027\u7684\u6574\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 concurrenthashset \u6765\u8bb0\u5f55\u5df2\u552e\u51fa\u7684\u7968\u53f7\uff0c\u5e76\u4f7f\u7528 atomicinteger \u6765\u8bb0\u5f55\u5269\u4f59\u7968\u6570\u3002\u5f53\u7ebf\u7a0b\u62a2\u7968\u65f6\uff0c\u5b83\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece concurrenthashset \u4e2d\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u7968\u53f7\uff0c\u5e76\u4e14\u5982\u679c\u7968\u53f7\u53ef\u7528\uff0c\u5219\u4f7f\u7528 atomicinteger \u9012\u51cf\u5269\u4f59\u7968\u6570\u3002 \u516c\u5e73\u9501\uff1a\u516c\u5e73\u9501\u662f\u4e00\u79cd\u9501\uff0c\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u7ebf\u7a0b\u4ee5\u7533\u8bf7\u9501\u7684\u987a\u5e8f\u83b7\u53d6\u9501\u3002\u5728\u62a2\u7968\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reentrantlock \u548c\u5176 fr=true \u9009\u9879\u6765\u5b9e\u73b0\u516c\u5e73\u9501\u3002\u8fd9\u6837\uff0c\u7ebf\u7a0b\u5c06\u6309\u987a\u5e8f\u83b7\u53d6\u9501\uff0c\u4ee5\u786e\u4fdd\u516c\u5e73\u6027\u3002\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u516c\u5e73\u9501\u5b9e\u73b0\u62a2\u7968\uff1a import java.util.concurrent.locks.ReentrantLock; public class FairTicket\u6436\u7968 { private static final int TOTAL_TICKETS = 10; private static final int TOTAL_USERS = 100; \/\/ \u4f7f\u7528\u516c\u5e73\u9501 private static ReentrantLock fairLock = new ReentrantLock(true); public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64879","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64879"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64879\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}