{"id":64502,"date":"2025-05-03T16:58:13","date_gmt":"2025-05-03T08:58:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64502\/"},"modified":"2025-05-03T16:58:13","modified_gmt":"2025-05-03T08:58:13","slug":"java-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%b5%8b%e8%af%95%e7%bb%93%e6%9e%9c%e4%b8%8d%e4%b8%80%e8%87%b4%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e7%ba%bf%e7%a8%8b%e6%b1%a0%e4%bc%9a%e5%af%bc%e8%87%b4%e7%ba%bf-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64502\/","title":{"rendered":"Java \u591a\u7ebf\u7a0b\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b\u6c60\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u800c\u5355\u72ec\u521b\u5efa\u7ebf\u7a0b\u5374\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173064349675787.jpg\" class=\"aligncenter\" title=\"Java \u591a\u7ebf\u7a0b\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b\u6c60\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u800c\u5355\u72ec\u521b\u5efa\u7ebf\u7a0b\u5374\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff1f\u63d2\u56fe\" alt=\"Java \u591a\u7ebf\u7a0b\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b\u6c60\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u800c\u5355\u72ec\u521b\u5efa\u7ebf\u7a0b\u5374\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>java \u591a\u7ebf\u7a0b\u4ee3\u7801\u7591\u95ee\uff1a\u9a8c\u8bc1\u7ebf\u7a0b\u4e0d\u5b89\u5168\u6d4b\u8bd5\u5dee\u5f02<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u9700\u8981\u6d4b\u8bd5\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u7c7b el\uff0c\u8be5\u7c7b\u5177\u6709\u4e00\u4e2a\u8ba1\u6570\u5668 count\u3002\u5f53\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6d4b\u8bd5\u65f6\uff0c\u7ed3\u679c\u4e0d\u4e3a 100\uff08\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff09\uff0c\u4f46\u4f7f\u7528\u53e6\u4e00\u6bb5\u4ee3\u7801\u6d4b\u8bd5\u65f6\uff0c\u7ed3\u679c\u59cb\u7ec8\u4e3a 100\uff08\u7ebf\u7a0b\u5b89\u5168\uff09\u3002<\/p>\n<p><strong>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u6d4b\u8bd5 1\uff08\u7ebf\u7a0b\u6c60\uff09\nexecutorservice executorservice = executors.newcachedthreadpool();\nfor (int i = 0; i &lt; threadsize; i++) {\n    executorservice.execute(() -&gt; el.add());\n}\n\n\/\/ \u6d4b\u8bd5 2\uff08\u7ebf\u7a0b\uff09\nthread[] threads = new thread[100];\nfor (int i = 0; i &lt; 100; i++) {\n    threads[i] = new thread(el::add);\n}\n\nthread.sleep(100); \/\/ \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\nsystem.out.println(el.get());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>el \u7c7b\uff1a<\/strong><\/p>\n<pre>public class El {\n    public int count = 0;\n\n    public void add() {\n        count += 1;\n    }\n\n    public int get() {\n        return count;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<p><strong>\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\u5931\u8d25\u7684\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u5728\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\u4e2d\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u521b\u5efa\u4e86\u8bb8\u591a\u7ebf\u7a0b\u6765\u5e76\u884c\u6267\u884c el.add \u65b9\u6cd5\u3002\u4f46\u662f\uff0cel.add \u65b9\u6cd5\u6267\u884c\u65f6\u95f4\u5f88\u77ed\uff0c\u53ef\u80fd\u5bfc\u81f4\u7ebf\u7a0b\u6ca1\u6709\u540c\u65f6\u8fd0\u884c\u3002\u56e0\u6b64\uff0c\u8ba1\u6570\u5668 count \u7684\u503c\u6ca1\u6709\u88ab\u6b63\u786e\u9012\u589e\uff0c\u5bfc\u81f4\u7ed3\u679c\u4e0d\u4e3a 100\u3002<\/p>\n<p><strong>\u7b2c\u4e8c\u4e2a\u6d4b\u8bd5\u6210\u529f\u7684\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>\u5728\u7b2c\u4e8c\u4e2a\u6d4b\u8bd5\u4e2d\uff0c\u4f7f\u7528 thread \u7c7b\u521b\u5efa\u4e86\u7ebf\u7a0b\uff0c\u800c\u4e0d\u662f\u7ebf\u7a0b\u6c60\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u53ea\u662f\u8c03\u7528 el.add \u65b9\u6cd5\uff0c\u6ca1\u6709\u5176\u4ed6\u4ee3\u7801\u6267\u884c\u3002\u56e0\u6b64\uff0c\u7ebf\u7a0b\u6267\u884c\u65f6\u95f4\u66f4\u957f\uff0c\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u90fd\u540c\u65f6\u8fd0\u884c\u5e76\u6b63\u786e\u66f4\u65b0 count \u503c\u3002<\/p>\n<p><strong>\u6539\u8fdb\u5efa\u8bae\uff1a<\/strong><\/p>\n<p><strong>1. \u589e\u52a0 el.add \u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\uff1a<\/strong><\/p>\n<p>\u5728 el.run \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5faa\u73af\uff0c\u6267\u884c\u591a\u4e2a count += 1 \u64cd\u4f5c\uff0c\u4ee5\u589e\u52a0\u5176\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<p><strong>2. \u6dfb\u52a0\u8ba1\u6570\u5668\u9012\u51cf\u64cd\u4f5c\uff1a<\/strong><\/p>\n<p>\u5728 el.run \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5faa\u73af\uff0c\u6267\u884c\u591a\u4e2a count -= 1 \u64cd\u4f5c\uff0c\u4ee5\u8fdb\u4e00\u6b65\u6d4b\u8bd5\u7ebf\u7a0b\u5b89\u5168\uff0c\u5e76\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b count \u503c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava \u591a\u7ebf\u7a0b\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b\u6c60\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u800c\u5355\u72ec\u521b\u5efa\u7ebf\u7a0b\u5374\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\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>java \u591a\u7ebf\u7a0b\u4ee3\u7801\u7591\u95ee\uff1a\u9a8c\u8bc1\u7ebf\u7a0b\u4e0d\u5b89\u5168\u6d4b\u8bd5\u5dee\u5f02 \u95ee\u9898\u63cf\u8ff0\uff1a \u9700\u8981\u6d4b\u8bd5\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u7c7b el\uff0c\u8be5\u7c7b\u5177\u6709\u4e00\u4e2a\u8ba1\u6570\u5668 count\u3002\u5f53\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6d4b\u8bd5\u65f6\uff0c\u7ed3\u679c\u4e0d\u4e3a 100\uff08\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff09\uff0c\u4f46\u4f7f\u7528\u53e6\u4e00\u6bb5\u4ee3\u7801\u6d4b\u8bd5\u65f6\uff0c\u7ed3\u679c\u59cb\u7ec8\u4e3a 100\uff08\u7ebf\u7a0b\u5b89\u5168\uff09\u3002 \u6d4b\u8bd5\u4ee3\u7801\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u6d4b\u8bd5 1\uff08\u7ebf\u7a0b\u6c60\uff09 executorservice executorservice = executors.newcachedthreadpool(); for (int i = 0; i &lt; threadsize; i++) { executorservice.execute(() -&gt; el.add()); } \/\/ \u6d4b\u8bd5 2\uff08\u7ebf\u7a0b\uff09 thread[] threads = new thread[100]; for (int i = 0; i &lt; 100; i++) { threads[i] = new thread(el::add); } thread.sleep(100); [&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-64502","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64502","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=64502"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64502\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}