{"id":35685,"date":"2024-11-26T15:31:59","date_gmt":"2024-11-26T07:31:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35685\/"},"modified":"2024-11-26T15:31:59","modified_gmt":"2024-11-26T07:31:59","slug":"i-%e6%98%af%e5%8e%9f%e5%ad%90%e6%93%8d%e4%bd%9c%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35685\/","title":{"rendered":"i++ \u662f\u539f\u5b50\u64cd\u4f5c\u5417\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173069702087952.jpg\" class=\"aligncenter\" title=\"i++ \u662f\u539f\u5b50\u64cd\u4f5c\u5417\uff1f\u63d2\u56fe\" alt=\"i++ \u662f\u539f\u5b50\u64cd\u4f5c\u5417\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>i++ \u7684\u539f\u5b50\u6027\u63a2\u8ba8<\/strong><\/p>\n<p>\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u539f\u5b50\u6027\u64cd\u4f5c\u662f\u6307\u4e0d\u53ef\u4e2d\u65ad\u7684\u57fa\u672c\u64cd\u4f5c\u5e8f\u5217\u3002\u90a3\u4e48\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u7b26 i++ \u662f\u5426\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u5462\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a\u5426<\/strong><\/p>\n<p>i++ \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u5b83\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd\u8bed\u6cd5\u7cd6\u3002\u4e3a\u4e86\u5b9e\u73b0 i++ \u7684\u529f\u80fd\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u4e09\u4e2a\u987a\u5e8f\u6267\u884c\u7684\u52a8\u4f5c\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6 i \u7684\u5f53\u524d\u503c<\/li>\n<li>\u5c06 1 \u52a0\u5230 i \u7684\u5f53\u524d\u503c<\/li>\n<li>\u5c06\u65b0\u7684\u503c\u8d4b\u7ed9 i<\/li>\n<\/ol>\n<p>\u8fd9\u4e09\u4e2a\u52a8\u4f5c\u5e76\u4e0d\u662f\u540c\u65f6\u6267\u884c\u7684\uff0c\u56e0\u6b64 i++ \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u8fd9\u4e00\u70b9\uff1a<\/p>\n<pre>int i = 0;\nThread t1 = new Thread(() -&gt; {\n    for (int j = 0; j &lt; 100000; j++) {\n        i++;\n    }\n});\nThread t2 = new Thread(() -&gt; {\n    for (int j = 0; j &lt; 100000; j++) {\n        i++;\n    }\n});\nt1.start();\nt2.start();\nt1.join();\nt2.join();\n\nSystem.out.println(i); \/\/ \u8f93\u51fa\u53ef\u80fd\u5c0f\u4e8e 200000<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c i++ \u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u90a3\u4e48\u6700\u7ec8\u8f93\u51fa\u5e94\u8be5\u662f 200000\u3002\u7136\u800c\uff0c\u7531\u4e8e\u975e\u539f\u5b50\u6027\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u4ea4\u66ff\u6267\u884c i++ \u7684\u4e09\u4e2a\u52a8\u4f5c\uff0c\u5bfc\u81f4\u6700\u7ec8\u503c\u4f4e\u4e8e\u9884\u671f\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fi++ \u662f\u539f\u5b50\u64cd\u4f5c\u5417\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>i++ \u7684\u539f\u5b50\u6027\u63a2\u8ba8 \u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u539f\u5b50\u6027\u64cd\u4f5c\u662f\u6307\u4e0d\u53ef\u4e2d\u65ad\u7684\u57fa\u672c\u64cd\u4f5c\u5e8f\u5217\u3002\u90a3\u4e48\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u7b26 i++ \u662f\u5426\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u5462\uff1f \u7b54\u6848\uff1a\u5426 i++ \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u5b83\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd\u8bed\u6cd5\u7cd6\u3002\u4e3a\u4e86\u5b9e\u73b0 i++ \u7684\u529f\u80fd\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u4e09\u4e2a\u987a\u5e8f\u6267\u884c\u7684\u52a8\u4f5c\uff1a \u83b7\u53d6 i \u7684\u5f53\u524d\u503c \u5c06 1 \u52a0\u5230 i \u7684\u5f53\u524d\u503c \u5c06\u65b0\u7684\u503c\u8d4b\u7ed9 i \u8fd9\u4e09\u4e2a\u52a8\u4f5c\u5e76\u4e0d\u662f\u540c\u65f6\u6267\u884c\u7684\uff0c\u56e0\u6b64 i++ \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u8fd9\u4e00\u70b9\uff1a int i = 0; Thread t1 = new Thread(() -&gt; { for (int j = 0; j &lt; 100000; j++) { i++; } }); Thread t2 = new Thread(() -&gt; { for [&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-35685","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35685","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=35685"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35685\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}