{"id":203630,"date":"2025-05-22T13:20:10","date_gmt":"2025-05-22T05:20:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203630\/"},"modified":"2025-05-22T13:20:10","modified_gmt":"2025-05-22T05:20:10","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%85%ac%e5%b9%b3%e6%a8%a1%e6%8b%9f-100-%e4%ba%ba%e6%8a%a2-10-%e5%bc%a0%e7%a5%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203630\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241114\/17315811396735d4d3bef7c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u516c\u5e73\u5730\u6a21\u62df\u591a\u7ebf\u7a0b\u62a2\u7968<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><br \/>\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u6a21\u62df 100 \u4e2a\u4eba\u62a2\u593a 10 \u5f20\u7968\uff0c\u540c\u65f6\u4fdd\u8bc1\u6bcf\u4e2a\u4eba\u90fd\u6709\u516c\u5e73\u7684\u673a\u4f1a\u83b7\u5f97\u7968\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u516c\u5e73\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>\u5e76\u53d1\u96c6\u5408<\/strong>\uff08\u4f8b\u5982 concurrenthashset\uff09\u6765\u5b58\u50a8\u53ef\u7528\u7684\u7968\uff0c\u5e76\u4f7f\u7528<strong>\u539f\u5b50\u53d8\u91cf<\/strong>\uff08\u4f8b\u5982 atomicinteger\uff09\u6765\u8ddf\u8e2a\u5df2\u552e\u51fa\u7684\u7968\u6570\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5f15\u5165<strong>\u516c\u5e73\u9501<\/strong>\uff08\u901a\u8fc7\u8bbe\u7f6e fair=true\uff09\uff0c\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u4eba\u90fd\u6709\u76f8\u540c\u7684\u673a\u4f1a\u83b7\u5f97\u9501\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import java.util.concurrent.ConcurrentHashMap;\nimport java.util.concurrent.atomic.AtomicInteger;\nimport java.util.concurrent.locks.ReentrantLock;\n\npublic class TicketGrabber {\n\n    private final ConcurrentHashMap&lt;Integer, Ticket&gt; availableTickets;\n    private final AtomicInteger soldTickets;\n    private final ReentrantLock lock = new ReentrantLock(true); \/\/ \u516c\u5e73\u9501\n\n    public TicketGrabber(int numTickets) {\n        this.availableTickets = new ConcurrentHashMap&lt;&gt;();\n        for (int i = 0; i &lt; numTickets; i++) {\n            availableTickets.put(i, new Ticket());\n        }\n        this.soldTickets = new AtomicInteger(0);\n    }\n\n    public Ticket grabTicket() {\n        \/\/ \u4e0a\u9501\n        lock.lock();\n        try {\n            \/\/ \u68c0\u67e5\u662f\u5426\u6709\u53ef\u7528\u7684\u7968\n            if (soldTickets.get() &gt;= availableTickets.size()) {\n                return null; \/\/ \u6240\u6709\u7968\u5df2\u552e\u51fa\n            }\n            \/\/ \u627e\u5230\u4e00\u5f20\u53ef\u7528\u7684\u7968\n            for (Integer ticketId : availableTickets.keySet()) {\n                if (availableTickets.containsKey(ticketId)) {\n                    Ticket ticket = availableTickets.get(ticketId);\n                    availableTickets.remove(ticketId); \/\/ \u6807\u8bb0\u4e3a\u5df2\u552e\u51fa\n                    soldTickets.incrementAndGet(); \/\/ \u589e\u52a0\u5df2\u552e\u51fa\u7684\u7968\u6570\n                    return ticket;\n                }\n            }\n            return null; \/\/ \u627e\u4e0d\u5230\u53ef\u7528\u7684\u7968\n        } finally {\n            \/\/ \u89e3\u9501\n            lock.unlock();\n        }\n    }\n\n    \/\/ ... \u5176\u4ed6\u4ee3\u7801 ...\n\n}<\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528\uff1a<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a ticketgrabber \u5bf9\u8c61\uff0c\u5e76\u4f20\u5165\u53ef\u7528\u7684\u7968\u6570\u3002<\/li>\n<li>\u4f7f\u7528 grabticket() \u65b9\u6cd5\u8ba9\u6bcf\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u62a2\u7968\u3002<\/li>\n<li>\u5982\u679c\u6709\u7968\u53ef\u7528\uff0c\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u5f20\u7968\uff0c\u5426\u5219\u8fd4\u56de null\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203630","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203630"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203630\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}