{"id":64890,"date":"2025-05-03T15:24:41","date_gmt":"2025-05-03T07:24:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64890\/"},"modified":"2025-05-03T15:24:41","modified_gmt":"2025-05-03T07:24:41","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-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64890\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173087389549100.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\u7968\uff1f\u63d2\u56fe\" \/><\/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 fr=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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u516c\u5e73\u6a21\u62df 100 \u4eba\u62a2 10 \u5f20\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>\u5982\u4f55\u516c\u5e73\u5730\u6a21\u62df\u591a\u7ebf\u7a0b\u62a2\u7968 \u95ee\u9898\u63cf\u8ff0\uff1a\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 \u89e3\u51b3\u65b9\u6848\uff1a \u4e3a\u4e86\u786e\u4fdd\u516c\u5e73\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e76\u53d1\u96c6\u5408\uff08\u4f8b\u5982 concurrenthashset\uff09\u6765\u5b58\u50a8\u53ef\u7528\u7684\u7968\uff0c\u5e76\u4f7f\u7528\u539f\u5b50\u53d8\u91cf\uff08\u4f8b\u5982 atomicinteger\uff09\u6765\u8ddf\u8e2a\u5df2\u552e\u51fa\u7684\u7968\u6570\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5f15\u5165\u516c\u5e73\u9501\uff08\u901a\u8fc7\u8bbe\u7f6e fr=true\uff09\uff0c\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u4eba\u90fd\u6709\u76f8\u540c\u7684\u673a\u4f1a\u83b7\u5f97\u9501\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; public class TicketGrabber { private final ConcurrentHashMap&lt;Integer, Ticket&gt; availableTickets; private final AtomicInteger soldTickets; private final ReentrantLock lock = new ReentrantLock(true); \/\/ \u516c\u5e73\u9501 public TicketGrabber(int numTickets) { this.availableTickets = new ConcurrentHashMap&lt;&gt;(); for (int i = 0; i &lt; [&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-64890","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64890","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=64890"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64890\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}