{"id":202569,"date":"2025-05-22T10:56:46","date_gmt":"2025-05-22T02:56:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202569\/"},"modified":"2025-05-22T10:56:46","modified_gmt":"2025-05-22T02:56:46","slug":"java-%e5%87%bd%e6%95%b0%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e6%80%a7%e7%9a%84%e8%a1%8c%e4%b8%9a%e6%a0%87%e5%87%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202569\/","title":{"rendered":"Java \u51fd\u6570\u7ebf\u7a0b\u5b89\u5168\u6027\u7684\u884c\u4e1a\u6807\u51c6"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u51fd\u6570\u7ebf\u7a0b\u5b89\u5168\u6027\u7684\u884c\u4e1a\u6807\u51c6<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u51fd\u6570\u7ebf\u7a0b\u5b89\u5168\u6027\u7684\u884c\u4e1a\u6807\u51c6\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u7ebf\u7a0b\u5b89\u5168\u51fd\u6570\u7684\u884c\u4e1a\u6807\u51c6\uff1a\u8bbe\u8ba1\u65e0\u72b6\u6001\u51fd\u6570\u3002\u4f7f\u7528\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u3002\u540c\u6b65\u8bbf\u95ee\u5171\u4eab\u72b6\u6001\u3002\u4f7f\u7528\u4e0d\u53ef\u53d8\u5bf9\u8c61\u3002\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u5bb9\u5668\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729918821671c77650b8e4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u7ebf\u7a0b\u5b89\u5168\u6027\u7684\u884c\u4e1a\u6807\u51c6<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5728 Java \u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u786e\u4fdd\u51fd\u6570\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u81f3\u5173\u91cd\u8981\u3002\u7ebf\u7a0b\u5b89\u5168\u6027\u662f\u6307\u51fd\u6570\u65e0\u72b6\u6001\u6216\u5176\u72b6\u6001\u7531\u8c03\u7528\u7ebf\u7a0b\u63a7\u5236\uff0c\u4ece\u800c\u9632\u6b62\u5e76\u53d1\u8bbf\u95ee\u9020\u6210\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u884c\u4e1a\u6807\u51c6<\/strong><\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\uff0c\u4e1a\u754c\u91c7\u7528\u4e86\u4ee5\u4e0b\u6807\u51c6\uff1a<\/p>\n<ul>\n<li><strong>\u65e0\u72b6\u6001\u51fd\u6570\uff1a<\/strong> \u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u4e0d\u5e94\u7ef4\u62a4\u4efb\u4f55\u72b6\u6001\u3002\u8fd9\u6d88\u9664\u4e86\u5e76\u53d1\u8bbf\u95ee\u7684\u98ce\u9669\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\uff1a<\/strong> \u6bcf\u4e2a\u8c03\u7528\u7ebf\u7a0b\u90fd\u7ef4\u62a4\u81ea\u5df1\u7684\u53d8\u91cf\u526f\u672c\uff0c\u5373\u4f7f\u662f\u9759\u6001\u65b9\u6cd5\u4e2d\u4e5f\u5982\u6b64\u3002\u8fd9\u53ef\u9632\u6b62\u7ebf\u7a0b\u4e4b\u95f4\u7684\u72b6\u6001\u4ea4\u53c9\u3002<\/li>\n<li><strong>\u540c\u6b65\u8bbf\u95ee\uff1a<\/strong> \u901a\u8fc7 synchronized \u5173\u952e\u5b57\u6216 ReentrantLock \u9501\u5b9a\u8d44\u6e90\uff0c\u786e\u4fdd\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u72b6\u6001\u3002<\/li>\n<li><strong>\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff1a<\/strong> \u4f7f\u7528\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08\u5982 String\u3001Integer\uff09\u53ef\u9632\u6b62\u72b6\u6001\u88ab\u610f\u5916\u4fee\u6539\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u5b89\u5168\u5bb9\u5668\uff1a<\/strong> \u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u5bb9\u5668\uff0c\u5982 ConcurrentHashMap\uff0c\u6765\u7ba1\u7406\u5e76\u53d1\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a BankAccount \u7c7b\uff0c\u5b83\u63d0\u4f9b\u5b58\u6b3e\u548c\u53d6\u6b3e\u64cd\u4f5c\uff1a<\/p>\n<pre>class BankAccount {\n\n    private double balance;\n\n    public void deposit(double amount) {\n        balance += amount;\n    }\n\n    public double withdraw(double amount) {\n        if (amount &lt;= balance) {\n            balance -= amount;\n            return amount;\n        }\n        throw new InsufficientFundsException();\n    }\n}<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cdeposit \u65b9\u6cd5\u53ef\u4ee5\u8bbe\u8ba1\u4e3a\u65e0\u72b6\u6001\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u7ef4\u62a4\u4efb\u4f55\u72b6\u6001\u3002\u4f46\u662f\uff0cwithdraw \u65b9\u6cd5\u9700\u8981\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff1a<\/p>\n<pre>public synchronized double withdraw(double amount) {\n    if (amount &lt;= balance) {\n        balance -= amount;\n        return amount;\n    }\n    throw new InsufficientFundsException();\n}<\/pre>\n<p>\u901a\u8fc7\u6dfb\u52a0 synchronized \u5173\u952e\u5b57\uff0c\u6211\u4eec\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u4f59\u989d\u548c\u66f4\u65b0\u5176\u72b6\u6001\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9075\u5faa\u8fd9\u4e9b\u884c\u4e1a\u6807\u51c6\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684 Java \u51fd\u6570\uff0c\u907f\u514d\u5e76\u53d1\u8bbf\u95ee\u9519\u8bef\uff0c\u5e76\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u51fd\u6570\u7ebf\u7a0b\u5b89\u5168\u6027\u7684\u884c\u4e1a\u6807&#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-202569","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202569","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=202569"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202569\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}