{"id":202540,"date":"2025-05-22T10:26:24","date_gmt":"2025-05-22T02:26:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202540\/"},"modified":"2025-05-22T10:26:24","modified_gmt":"2025-05-22T02:26:24","slug":"%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8%e7%94%a8%e4%ba%8e%e4%bc%98%e5%8c%96-java-%e5%87%bd%e6%95%b0%e6%89%a7%e8%a1%8c%e6%95%88%e7%8e%87%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202540\/","title":{"rendered":"\u662f\u5426\u5b58\u5728\u7528\u4e8e\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u6700\u4f73\u5b9e\u8df5\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u662f\u5426\u5b58\u5728\u7528\u4e8e\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u6700\u4f73\u5b9e\u8df5\uff1f<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u662f\u5426\u5b58\u5728\u7528\u4e8e\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u6700\u4f73\u5b9e\u8df5\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>\u4e3a\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\uff0c\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a1. \u7f13\u5b58\u6570\u636e\uff1b2. \u5229\u7528\u5e76\u884c\u6027\uff1b3. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff1b4. \u4f7f\u7528\u81ea\u52a8\u88c5\u7bb1\u548c\u62c6\u7bb1\uff1b5. \u907f\u514d\u53cd\u5c04\u3002\u901a\u8fc7\u5b9e\u65bd\u8fd9\u4e9b\u63aa\u65bd\uff0c\u53ef\u663e\u8457\u63d0\u9ad8 Java \u51fd\u6570\u7684\u6267\u884c\u6548\u7387\uff0c\u4ece\u800c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729916000671c6c609b696.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u6700\u4f73\u5b9e\u8df5<\/h2>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>Java \u662f\u6027\u80fd\u826f\u597d\u7684\u8bed\u8a00\uff0c\u4f46\u901a\u8fc7\u91c7\u7528\u6b63\u786e\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u5176\u6267\u884c\u6548\u7387\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u51e0\u4e2a\u589e\u5f3a Java \u51fd\u6570\u6027\u80fd\u7684\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n<p><strong>1. \u7f13\u5b58\u6570\u636e<\/strong><\/p>\n<p>\u907f\u514d\u91cd\u590d\u6602\u8d35\u7684\u64cd\u4f5c\uff0c\u5982\u6570\u636e\u5e93\u67e5\u8be2\u6216\u6587\u4ef6\u8bfb\u53d6\u3002\u901a\u8fc7\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u5728\u6bcf\u6b21\u9700\u8981\u6570\u636e\u65f6\u91cd\u65b0\u83b7\u53d6\u6570\u636e\u3002\u4f7f\u7528 <code>HashMap<\/code>\u3001<code>ConcurrentHashMap<\/code> \u6216\u7b2c\u4e09\u65b9\u7f13\u5b58\u5e93\uff0c\u5982 Caffeine \u6216 Ehcache\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>private static Map&lt;String, Object&gt; cache = new HashMap&lt;&gt;();\n\npublic Object getData(String key) {\n    Object value = cache.get(key);\n    if (value == null) {\n        value = expensiveOperation();\n        cache.put(key, value);\n    }\n    return value;\n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u5e76\u884c\u6027<\/strong><\/p>\n<p>\u5145\u5206\u5229\u7528\u591a\u6838 CPU \u7684\u4f18\u52bf\uff0c\u901a\u8fc7\u4e3a\u72ec\u7acb\u4efb\u52a1\u521b\u5efa\u7ebf\u7a0b\u6216\u4f7f\u7528\u5e76\u884c\u6d41\u3002\u4f7f\u7528 <code>ExecutorService<\/code> \u6216 <code>CompletableFuture<\/code> \u7b49 Java \u5e76\u53d1\u5e93\u6765\u7ba1\u7406\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ExecutorService executor = Executors.newFixedThreadPool(4);\n\nList&lt;Integer&gt; numbers = ...;\n\nList&lt;Future&lt;Integer&gt;&gt; results = executor.invokeAll(numbers.stream()\n    .map(n -&gt; CompletableFuture.supplyAsync(() -&gt; doubleNumber(n)))\n    .toList());<\/pre>\n<p><strong>3. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c<\/strong><\/p>\n<p>\u5b57\u7b26\u4e32\u64cd\u4f5c\u662f\u8017\u65f6\u7684\u3002\u5f53\u9700\u8981\u9891\u7e41\u5730\u8fde\u63a5\u6216\u64cd\u4f5c\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 <code>StringBuilder<\/code> \u6216 <code>StringBuffer<\/code> \u6765\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>String result = \"\";\nfor (int i = 0; i &lt; 100000; i++) {\n    result += i;\n}\n\n\/\/ \u6539\u7528 StringBuilder\nStringBuilder resultBuilder = new StringBuilder();\nfor (int i = 0; i &lt; 100000; i++) {\n    resultBuilder.append(i);\n}\nString result = resultBuilder.toString();<\/pre>\n<p><strong>4. \u4f7f\u7528\u81ea\u52a8\u88c5\u7bb1\u548c\u62c6\u7bb1<\/strong><\/p>\n<p>\u5728\u57fa\u672c\u7c7b\u578b\u548c\u5176\u5bf9\u5e94\u7684\u5305\u88c5\u7c7b\u4e4b\u95f4\u9891\u7e41\u8f6c\u6362\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u8f6c\u6362\uff0c\u5e76\u4f7f\u7528 <code>Optional<\/code> \u6216 <code>IntStream<\/code> \u7b49\u66ff\u4ee3\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u907f\u514d\u81ea\u52a8\u88c5\u7bb1\u548c\u62c6\u7bb1\nint[] numbers = ...;\n\nfor (int n : numbers) {\n    \/\/ \u64cd\u4f5c n\n}\n\n\/\/ \u4f7f\u7528 IntStream\nIntStream.of(numbers).forEach(n -&gt; {\n    \/\/ \u64cd\u4f5c n\n});<\/pre>\n<p><strong>5. \u907f\u514d\u53cd\u5c04<\/strong><\/p>\n<p>\u53cd\u5c04\u6d89\u53ca\u5728\u8fd0\u884c\u65f6\u83b7\u53d6\u548c\u64cd\u4f5c\u7c7b\u7684\u5143\u6570\u636e\uff0c\u8fd9\u4f1a\u5e26\u6765\u5f00\u9500\u3002\u53ea\u6709\u5728\u7edd\u5bf9\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u53cd\u5c04\uff0c\u5e76\u8003\u8651\u4f7f\u7528\u7f13\u5b58\u6216\u4ee3\u7801\u751f\u6210\u6280\u672f\u6765\u51cf\u5c11\u5f00\u9500\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u907f\u514d\u53cd\u5c04\nClass&lt;?&gt; clazz = Class.forName(\"com.example.MyClass\");\n\n\/\/ \u8003\u8651\u4f7f\u7528\u4ee3\u7801\u751f\u6210\nProxyGenerator generator = new ProxyGenerator();\nbyte[] classBytes = generator.generateProxyClass(clazz);\nClass&lt;?&gt; proxyClass = new ClassLoader().defineClass(classBytes);<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5b9e\u65bd\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 Java \u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u3002\u901a\u8fc7\u7f13\u5b58\u3001\u5e76\u884c\u6027\u4f18\u5316\u3001\u51cf\u5c11\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u907f\u514d\u81ea\u52a8\u88c5\u7bb1\u548c\u62c6\u7bb1\u4ee5\u53ca\u51cf\u5c11\u53cd\u5c04\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u66f4\u5feb\u7684\u4ee3\u7801\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u662f\u5426\u5b58\u5728\u7528\u4e8e\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u6700\u4f73\u5b9e\u8df5\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u5426\u5b58\u5728\u7528\u4e8e\u4f18\u5316 Java \u51fd\u6570&#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-202540","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202540","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=202540"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202540\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}