{"id":1,"date":"2024-11-20T12:47:33","date_gmt":"2024-11-20T04:47:33","guid":{"rendered":"http:\/\/genetop.top\/?p=1"},"modified":"2024-11-20T18:55:56","modified_gmt":"2024-11-20T10:55:56","slug":"synchronized-java","status":"publish","type":"post","link":"https:\/\/genetop.top\/index.php\/2024\/11\/20\/synchronized-java\/","title":{"rendered":"\u4e3a\u4ec0\u4e48Java\u9700\u8981\u5bf9Synchronized\u8fdb\u884c\u81ea\u65cb\u4f18\u5316"},"content":{"rendered":"\r\n<p>\u5728Java\u7684\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c<code>synchronized<\/code>\u662f\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u91cd\u8981\u673a\u5236\u3002\u4f20\u7edf\u5b9e\u73b0\u901a\u8fc7\u91cd\u91cf\u7ea7\u9501\uff08\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e92\u65a5\u91cf\uff09\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u4f46\u5b58\u5728\u8f83\u5927\u7684\u6027\u80fd\u5f00\u9500\u3002\u4e3a\u4e86\u63d0\u5347\u6027\u80fd\uff0c\u73b0\u4ee3JVM\uff08\u5982HotSpot\uff09\u5f15\u5165\u4e86**\u81ea\u65cb\u9501\uff08Spin Lock\uff09**\u6765\u4f18\u5316<code>Synchronized<\/code>\u7684\u8868\u73b0\u3002\u672c\u6587\u5206\u6790\u4e86<code>Synchronized<\/code>\u7684\u8fd0\u884c\u539f\u7406\u3001\u81ea\u65cb\u7684\u5fc5\u8981\u6027\u4ee5\u53ca\u5176\u4f18\u7f3a\u70b9\u548c\u5b9e\u9645\u5e94\u7528\u3002<\/p>\r\n\r\n\r\n\r\n<p><code>Synchronized<\/code>\u662fJava\u7684\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u4fdd\u8bc1\u7ebf\u7a0b\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u4e92\u65a5\u8bbf\u95ee\u3002\u5728\u65e9\u671f\u5b9e\u73b0\u4e2d\uff0cJava\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e92\u65a5\u9501\uff08Mutex\uff09\u5b9e\u73b0\uff0c\u4f46\u7531\u4e8e\u7ebf\u7a0b\u6302\u8d77\u548c\u5524\u9192\u6d89\u53ca\u5185\u6838\u6001\u548c\u7528\u6237\u6001\u5207\u6362\uff0c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u5728\u591a\u6838CPU\u73af\u5883\u4e0b\uff0c\u77ed\u671f\u7ade\u4e89\u7684\u7ebf\u7a0b\u5207\u6362\u4ee3\u4ef7\u9ad8\u6602\u3002\u56e0\u6b64\uff0cJVM\u5f15\u5165\u4e86\u57fa\u4e8e\u7528\u6237\u6001\u7684\u81ea\u65cb\u9501\uff0c\u5728\u4e00\u5b9a\u6761\u4ef6\u4e0b\u907f\u514d\u7ebf\u7a0b\u8fdb\u5165\u5185\u6838\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\r\n\r\n\r\n\r\n<h1><strong><code>Synchronized<\/code>\u7684\u5b9e\u73b0\u673a\u5236<\/strong><\/h1>\r\n\r\n\r\n\r\n<p>\u5728\u73b0\u4ee3JVM\u4e2d\uff0c<code>Synchronized<\/code>\u901a\u8fc7<strong>\u5bf9\u8c61\u5934<\/strong>\u548c<strong>\u9501\u72b6\u6001<\/strong>\u5b9e\u73b0\u9501\u7ba1\u7406\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u72b6\u6001\uff1a<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li style=\"list-style-type: none;\">\r\n<ol class=\"wp-block-list\">\r\n<li><strong>\u65e0\u9501\uff08No Locking\uff09<\/strong>\uff1a\u5bf9\u8c61\u6ca1\u6709\u88ab\u7ebf\u7a0b\u7ade\u4e89\u8bbf\u95ee\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u504f\u5411\u9501\uff08Biased Locking\uff09<\/strong>\uff1a\u7ebf\u7a0b\u72ec\u5360\u9501\uff0c\u51cf\u5c11CAS\u64cd\u4f5c\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u8f7b\u91cf\u7ea7\u9501\uff08Lightweight Locking\uff09<\/strong>\uff1a\u5229\u7528CAS\u5b9e\u73b0\u65e0\u963b\u585e\u7ade\u4e89\u3002<\/li>\r\n<li><strong>\u91cd\u91cf\u7ea7\u9501\uff08Heavyweight Locking\uff09<\/strong>\uff1a\u7ebf\u7a0b\u8fdb\u5165\u5185\u6838\u6001\uff0c\u901a\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u4e92\u65a5\u9501\u534f\u8c03\u8bbf\u95ee\u3002<\/li>\r\n<\/ol>\r\n<\/li>\r\n<\/ol>\r\n<ol class=\"wp-block-list\"><\/ol>\r\n\r\n\r\n\r\n<p>\u81ea\u65cb\u4f18\u5316\u4e3b\u8981\u4f5c\u7528\u4e8e\u8f7b\u91cf\u7ea7\u9501\u9636\u6bb5\u3002<\/p>\r\n\r\n\r\n\r\n<h1><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u81ea\u65cb\u4f18\u5316<\/strong><\/h1>\r\n\r\n\r\n\r\n<ol>\r\n<li><strong>\u7ebf\u7a0b\u5207\u6362\u5f00\u9500\u5927<\/strong>\uff1a \u7ebf\u7a0b\u88ab\u963b\u585e\u65f6\uff0c\u6d89\u53ca\u4e0a\u4e0b\u6587\u5207\u6362\uff08Context Switch\uff09\uff0c\u5305\u62ec\u72b6\u6001\u4fdd\u5b58\u3001\u8c03\u5ea6\u3001\u6062\u590d\uff0c\u6d88\u8017\u5927\u91cf\u65f6\u95f4\u3002\r\n\r\n<\/li>\r\n<li><strong>\u591a\u6838CPU\u652f\u6301\u5e76\u53d1<\/strong>\uff1a \u5728\u73b0\u4ee3\u591a\u6838\u73af\u5883\u4e2d\uff0c\u7ebf\u7a0b\u77ed\u671f\u5185\u7684\u7ade\u4e89\u53ef\u4ee5\u901a\u8fc7\u5fd9\u7b49\u5f85\uff08Busy Waiting\uff09\u5feb\u901f\u89e3\u51b3\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u963b\u585e\u3002\r\n\r\n<\/li>\r\n<li><strong>\u77ed\u6682\u9501\u4e89\u7528\u573a\u666f\u591a<\/strong>\uff1a \u5927\u591a\u6570\u9501\u7ade\u4e89\u662f\u77ed\u671f\u7684\uff0c\u6bd4\u5982\u4e00\u4e2a\u9501\u4ec5\u7528\u4e8e\u4fdd\u62a4\u5c11\u91cf\u6570\u636e\u64cd\u4f5c\uff0c\u81ea\u65cb\u80fd\u6709\u6548\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4\u3002<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\"><strong>\u81ea\u65cb\u9501\u7684\u5b9e\u73b0<\/strong><\/h1>\r\n\r\n\r\n\r\n<p>JVM\u4e2d\u7684\u81ea\u65cb\u9501\u5b9e\u73b0\u57fa\u4e8e\u5faa\u73af\u68c0\u67e5\u9501\u72b6\u6001\uff0c\u5f53\u9501\u53ef\u7528\u65f6\u76f4\u63a5\u83b7\u53d6\u9501\uff0c\u800c\u4e0d\u662f\u8fdb\u5165\u963b\u585e\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u5229\u7528CPU\u65f6\u95f4\u6362\u53d6\u7ebf\u7a0b\u5207\u6362\u7684\u4ee3\u4ef7\u3002<\/p>\r\n\r\n\r\n\r\n<p><strong>\u5178\u578b\u5b9e\u73b0<\/strong>\uff1a<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li><strong>\u81ea\u65cb\u5c1d\u8bd5<\/strong>\uff1a \u81ea\u65cb\u8fc7\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u53cd\u590d\u68c0\u67e5\u9501\u662f\u5426\u53ef\u7528\uff0c\u76f4\u5230\u8d85\u65f6\u6216\u83b7\u53d6\u6210\u529f\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u81ea\u65cb\u65f6\u95f4\u9650\u5236<\/strong>\uff1a \u81ea\u65cb\u65f6\u95f4\u8bbe\u5b9a\u4e3a\u4e00\u4e2a\u4e0a\u9650\uff0c\u9632\u6b62\u7ebf\u7a0b\u957f\u671f\u5360\u7528CPU\u8d44\u6e90\u3002<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\"><strong>\u81ea\u65cb\u9501\u7684\u4f18\u7f3a\u70b9<\/strong><\/h1>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>\u4f18\u70b9\uff1a<\/strong><\/h2>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li><strong>\u6027\u80fd\u63d0\u5347<\/strong>\uff1a \u5728\u9501\u7ade\u4e89\u65f6\u95f4\u8f83\u77ed\u7684\u573a\u666f\uff0c\u81ea\u65cb\u80fd\u663e\u8457\u51cf\u5c11\u7ebf\u7a0b\u5207\u6362\u7684\u5f00\u9500\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u7528\u6237\u6001\u64cd\u4f5c<\/strong>\uff1a \u81ea\u65cb\u907f\u514d\u4e86\u7cfb\u7edf\u8c03\u7528\uff0c\u51cf\u5c11\u4e86\u7ebf\u7a0b\u4ece\u7528\u6237\u6001\u5230\u5185\u6838\u6001\u7684\u5207\u6362\u6b21\u6570\u3002<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>\u7f3a\u70b9\uff1a<\/strong><\/h2>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li><strong>\u6d6a\u8d39CPU\u8d44\u6e90<\/strong>\uff1a \u81ea\u65cb\u662f\u5fd9\u7b49\u5f85\u673a\u5236\uff0c\u5982\u679c\u9501\u4e89\u7528\u65f6\u95f4\u8f83\u957f\uff0c\u4f1a\u6d6a\u8d39\u5927\u91cfCPU\u65f6\u95f4\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u4e0d\u9002\u5408\u9ad8\u7ade\u4e89\u573a\u666f<\/strong>\uff1a \u9ad8\u7ade\u4e89\u573a\u666f\u4e2d\uff0c\u81ea\u65cb\u53ef\u80fd\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u9965\u997f\uff0c\u7cfb\u7edf\u541e\u5410\u91cf\u4e0b\u964d\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u4f9d\u8d56\u786c\u4ef6\u73af\u5883<\/strong>\uff1a \u591a\u6838CPU\u652f\u6301\u4e0b\u6548\u679c\u663e\u8457\uff0c\u5355\u6838\u73af\u5883\u53ef\u80fd\u9002\u5f97\u5176\u53cd\u3002<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\"><strong>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/strong><\/h1>\r\n\r\n\r\n\r\n<p><strong>\u81ea\u65cb\u9501\u9002\u5408\u4ee5\u4e0b\u573a\u666f\uff1a<\/strong><\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li><strong>\u8f7b\u91cf\u7ea7\u9501<\/strong>\uff1a\u9501\u7ade\u4e89\u65f6\u95f4\u8f83\u77ed\uff0c\u901a\u5e38\u5728\u5fae\u79d2\u6216\u6beb\u79d2\u7ea7\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u9ad8\u5e76\u53d1\u573a\u666f<\/strong>\uff1a\u5982\u9ad8\u9891\u8bfb\u5199\u64cd\u4f5c\u4e2d\uff0c\u77ed\u65f6\u95f4\u9501\u7ade\u4e89\u9891\u7e41\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u591a\u6838CPU\u7cfb\u7edf<\/strong>\uff1a\u591a\u6838\u67b6\u6784\u4e0b\uff0c\u81ea\u65cb\u80fd\u6709\u6548\u5229\u7528\u5e76\u53d1\u4f18\u52bf\u3002<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\"><strong>JVM\u5bf9\u81ea\u65cb\u7684\u4f18\u5316<\/strong><\/h1>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li><strong>\u81ea\u9002\u5e94\u81ea\u65cb<\/strong>\uff1a HotSpot JVM\u4e2d\u7684<strong>\u81ea\u9002\u5e94\u81ea\u65cb\u9501<\/strong>\u4f1a\u6839\u636e\u9501\u4e89\u7528\u7684\u5386\u53f2\u60c5\u51b5\u52a8\u6001\u8c03\u6574\u81ea\u65cb\u65f6\u95f4\u3002\u7ade\u4e89\u6fc0\u70c8\u65f6\u51cf\u5c11\u81ea\u65cb\uff0c\u7ade\u4e89\u8f83\u5c11\u65f6\u5ef6\u957f\u81ea\u65cb\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u504f\u5411\u9501\u548c\u8f7b\u91cf\u7ea7\u9501\u7ed3\u5408<\/strong>\uff1a \u81ea\u65cb\u4f18\u5316\u901a\u5e38\u4e0e\u504f\u5411\u9501\u548c\u8f7b\u91cf\u7ea7\u9501\u8054\u5408\u4f7f\u7528\uff0c\u51cf\u5c11\u9501\u5347\u7ea7\u7684\u9891\u7387\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u907f\u514d\u7ebf\u7a0b\u9965\u997f<\/strong>\uff1a JVM\u4f1a\u5728\u81ea\u65cb\u65f6\u95f4\u8fc7\u957f\u65f6\u5f3a\u5236\u7ebf\u7a0b\u963b\u585e\uff0c\u907f\u514d\u9965\u997f\u73b0\u8c61\u3002<\/li>\r\n<\/ol>\r\n","protected":false},"excerpt":{"rendered":"<p>\u5728Java\u7684\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0csynchronized\u662f\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u91cd\u8981\u673a\u5236\u3002\u4f20\u7edf\u5b9e\u73b0\u901a\u8fc7\u91cd\u91cf\u7ea7\u9501\uff08\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e92 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[10],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-java","category-language","tag-java"],"_links":{"self":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":4,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}