{"id":5357,"date":"2022-08-31T13:24:26","date_gmt":"2022-08-31T05:24:26","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5357"},"modified":"2022-08-31T13:24:26","modified_gmt":"2022-08-31T05:24:26","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bb%8ejava15%e5%bc%80%e5%a7%8b%e8%a6%81%e7%a6%81%e7%94%a8%e5%81%8f%e5%90%91%e9%94%81%e4%ba%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5357","title":{"rendered":"\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock0.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 JDK1.5 \u4e4b\u524d\uff0c\u9762\u5bf9 Java \u5e76\u53d1\u95ee\u9898\uff0c synchronized \u662f\u4e00\u62db\u9c9c\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u666e\u901a\u540c\u6b65\u65b9\u6cd5\uff0c\u9501\u4e0a\u5f53\u524d\u5b9e\u4f8b\u5bf9\u8c61<\/li><li>\u9759\u6001\u540c\u6b65\u65b9\u6cd5\uff0c\u9501\u4e0a\u5f53\u524d\u7c7b Class \u5bf9\u8c61<\/li><li>\u540c\u6b65\u5757\uff0c\u9501\u4e0a\u62ec\u53f7\u91cc\u9762\u914d\u7f6e\u7684\u5bf9\u8c61<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u62ff\u540c\u6b65\u5757\u6765\u4e3e\u4f8b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void test(){\n  synchronized (object) {\n    i++;\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ecf\u8fc7<code>javap -v<\/code>\u7f16\u8bd1\u540e\u7684\u6307\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock1.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><code>monitorenter<\/code>\u6307\u4ee4\u662f\u5728\u7f16\u8bd1\u540e\u63d2\u5165\u5230\u540c\u6b65\u4ee3\u7801\u5757\u7684\u5f00\u59cb\u4f4d\u7f6e\uff1b<code>monitorexit<\/code>\u662f\u63d2\u5165\u5230\u65b9\u6cd5\u7ed3\u675f\u548c\u5f02\u5e38\u7684\u4f4d\u7f6e(\u5b9e\u9645\u9690\u85cf\u4e86try-finally)\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a monitor \u4e0e\u4e4b\u5173\u8054\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5230 monitorenter \u6307\u4ee4\u65f6\uff0c\u5c31\u4f1a\u83b7\u5f97\u5bf9\u8c61\u6240\u5bf9\u5e94\u7684<code>monitor<\/code>\u7684\u6240\u6709\u6743\uff0c\u4e5f\u5c31\u83b7\u5f97\u5230\u4e86\u5bf9\u8c61\u7684\u9501<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5230\u540c\u6b65\u5757\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u5b83\u6ca1\u6709\u5bf9\u5e94<code>monitor<\/code>\u7684\u6240\u6709\u6743\uff0c\u5c31\u4f1a\u88ab\u963b\u585e\uff0c\u6b64\u65f6\u63a7\u5236\u6743\u53ea\u80fd\u4ea4\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4e5f\u5c31\u4f1a\u4ece<code>user mode<\/code>\u5207\u6362\u5230<code>kernel mode<\/code>, \u7531\u64cd\u4f5c\u7cfb\u7edf\u6765\u8d1f\u8d23\u7ebf\u7a0b\u95f4\u7684\u8c03\u5ea6\u548c\u7ebf\u7a0b\u7684\u72b6\u6001\u53d8\u66f4, \u9700\u8981\u9891\u7e41\u7684\u5728\u8fd9\u4e24\u4e2a\u6a21\u5f0f\u4e0b\u5207\u6362\uff08<code>\u4e0a\u4e0b\u6587\u8f6c\u6362<\/code>\uff09\u3002\u8fd9\u79cd\u6709\u70b9\u7ade\u4e89\u5c31\u627e\u5185\u6838\u7684\u884c\u4e3a\u5f88\u4e0d\u597d\uff0c\u4f1a\u5f15\u8d77\u5f88\u5927\u7684\u5f00\u9500\uff0c\u6240\u4ee5\u5927\u5bb6\u90fd\u53eb\u5b83\u91cd\u91cf\u7ea7\u9501\uff0c\u81ea\u7136\u6548\u7387\u4e5f\u5f88\u4f4e\uff0c\u8fd9\u4e5f\u5c31\u7ed9\u5f88\u591a\u7ae5\u978b\u7559\u4e0b\u4e86\u4e00\u4e2a\u6839\u6df1\u8482\u56fa\u7684\u5370\u8c61\uff1a<code>synchronized\u5173\u952e\u5b57\u76f8\u6bd4\u4e8e\u5176\u4ed6\u540c\u6b65\u673a\u5236\u6027\u80fd\u4e0d\u597d<\/code>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9501\u7684\u6f14\u53d8<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u5230 JDK1.6\uff0c\u8981\u600e\u6837\u4f18\u5316\u624d\u80fd\u8ba9\u9501\u53d8\u7684\u8f7b\u91cf\u7ea7\u4e00\u4e9b\uff1f\u7b54\u6848\u5c31\u662f\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f7b\u91cf\u7ea7\u9501\uff1aCPU CAS<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c CPU \u901a\u8fc7\u7b80\u5355\u7684 CAS \u80fd\u5904\u7406\u52a0\u9501\/\u91ca\u653e\u9501\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u4e0a\u4e0b\u6587\u7684\u5207\u6362\uff0c\u8f83\u91cd\u91cf\u7ea7\u9501\u800c\u8a00\u81ea\u7136\u5c31\u8f7b\u4e86\u5f88\u591a\u3002\u4f46\u662f\u5f53\u7ade\u4e89\u5f88\u6fc0\u70c8\uff0cCAS \u5c1d\u8bd5\u518d\u591a\u4e5f\u662f\u6d6a\u8d39 CPU\uff0c\u6743\u8861\u4e00\u4e0b\uff0c\u4e0d\u5982\u5347\u7ea7\u6210\u91cd\u91cf\u7ea7\u9501\uff0c\u963b\u585e\u7ebf\u7a0b\u6392\u961f\u7ade\u4e89\uff0c\u4e5f\u5c31\u6709\u4e86\u8f7b\u91cf\u7ea7\u9501\u5347\u7ea7\u6210\u91cd\u91cf\u7ea7\u9501\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock2.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u5458\u5728\u8ffd\u6c42\u6781\u81f4\u7684\u9053\u8def\u4e0a\u662f\u6c38\u65e0\u6b62\u5883\u7684\uff0cHotSpot \u7684\u4f5c\u8005\u7ecf\u8fc7\u7814\u7a76\u53d1\u73b0\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9501\u4e0d\u4ec5\u4e0d\u5b58\u5728\u591a\u7ebf\u7a0b\u7ade\u4e89\uff0c\u800c\u4e14\u603b\u662f\u7531<code>\u540c\u4e00\u4e2a\u7ebf\u7a0b<\/code>\u591a\u6b21\u83b7\u5f97\uff0c\u540c\u4e00\u4e2a\u7ebf\u7a0b\u53cd\u590d\u83b7\u53d6\u9501\uff0c\u5982\u679c\u8fd8\u6309\u7167\u8f7b\u91cf\u7ea7\u9501\u7684\u65b9\u5f0f\u83b7\u53d6\u9501\uff08CAS\uff09\uff0c\u4e5f\u662f\u6709\u4e00\u5b9a\u4ee3\u4ef7\u7684\uff0c\u5982\u4f55\u8ba9\u8fd9\u4e2a\u4ee3\u4ef7\u66f4\u5c0f\u4e00\u4e9b\u5462\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u504f\u5411\u9501<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u504f\u5411\u9501\u5b9e\u9645\u5c31\u662f\u9501\u5bf9\u8c61\u6f5c\u610f\u8bc6\u300c\u504f\u5fc3\u300d\u540c\u4e00\u4e2a\u7ebf\u7a0b\u6765\u8bbf\u95ee\uff0c\u8ba9\u9501\u5bf9\u8c61\u8bb0\u4f4f\u7ebf\u7a0b ID\uff0c\u5f53\u7ebf\u7a0b\u518d\u6b21\u83b7\u53d6\u9501\u65f6\uff0c\u4eae\u51fa\u8eab\u4efd\uff0c\u5982\u679c\u540c\u4e00\u4e2a ID \u76f4\u63a5\u5c31\u83b7\u53d6\u9501\u5c31\u597d\u4e86\uff0c\u662f\u4e00\u79cd<code>load-and-test<\/code>\u7684\u8fc7\u7a0b\uff0c\u76f8\u8f83 CAS \u81ea\u7136\u53c8\u8f7b\u91cf\u7ea7\u4e86\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u662f\u591a\u7ebf\u7a0b\u73af\u5883\uff0c\u4e5f\u4e0d\u53ef\u80fd\u53ea\u662f\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e00\u76f4\u83b7\u53d6\u8fd9\u4e2a\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u662f\u8981\u5e72\u6d3b\u7684\uff0c\u5982\u679c\u51fa\u73b0\u591a\u4e2a\u7ebf\u7a0b\u7ade\u4e89\u7684\u60c5\u51b5\uff0c\u4e5f\u5c31\u6709\u4e86\u504f\u5411\u9501\u5347\u7ea7\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock3.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u53ef\u4ee5\u5148\u601d\u8003\u4e00\u4e0b\uff1a\u504f\u5411\u9501\u53ef\u4ee5\u7ed5\u8fc7\u8f7b\u91cf\u7ea7\u9501\uff0c\u76f4\u63a5\u5347\u7ea7\u5230\u91cd\u91cf\u7ea7\u9501\u5417\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u90fd\u662f\u540c\u4e00\u4e2a\u9501\u5bf9\u8c61\uff0c\u5374\u6709\u591a\u79cd\u9501\u72b6\u6001\uff0c\u5176\u76ee\u7684\u663e\u800c\u6613\u89c1\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u5360\u7528\u7684\u8d44\u6e90\u8d8a\u5c11\uff0c\u7a0b\u5e8f\u6267\u884c\u7684\u901f\u5ea6\u8d8a\u5feb\u275e<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u504f\u5411\u9501\uff0c\u8f7b\u91cf\u9501\uff0c\u5b83\u4fe9\u90fd\u4e0d\u4f1a\u8c03\u7528\u7cfb\u7edf\u4e92\u65a5\u91cf\uff08Mutex Lock\uff09\uff0c\u53ea\u662f\u4e3a\u4e86\u63d0\u5347\u6027\u80fd\uff0c\u591a\u51fa\u7684\u4e24\u79cd\u9501\u7684\u72b6\u6001\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u91c7\u53d6\u6700\u5408\u9002\u7684\u7b56\u7565\uff0c\u6240\u4ee5\u53ef\u4ee5\u603b\u7ed3\u6027\u7684\u8bf4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u504f\u5411\u9501\uff1a\u65e0\u7ade\u4e89\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u91c7\u7528\u504f\u5411\u9501<\/li><li>\u8f7b\u91cf\u7ea7\u9501\uff1a\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u4ea4\u66ff\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u91c7\u7528\u8f7b\u91cf\u7ea7\u9501<\/li><li>\u91cd\u91cf\u7ea7\u9501\uff1a\u591a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u4ea4\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4e92\u65a5\u91cf\u6765\u5904\u7406<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u8fd9\u91cc\uff0c\u5927\u5bb6\u5e94\u8be5\u7406\u89e3\u4e86\u5168\u5c40\u5927\u6846\uff0c\u4f46\u4ecd\u7136\u4f1a\u6709\u5f88\u591a\u7591\u95ee\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u9501\u5bf9\u8c61\u662f\u5728\u54ea\u5b58\u50a8\u7ebf\u7a0b ID \u624d\u53ef\u4ee5\u8bc6\u522b\u540c\u4e00\u4e2a\u7ebf\u7a0b\u7684\uff1f<\/li><li>\u6574\u4e2a\u5347\u7ea7\u8fc7\u7a0b\u662f\u5982\u4f55\u8fc7\u6e21\u7684\uff1f<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u60f3\u7406\u89e3\u8fd9\u4e9b\u95ee\u9898\uff0c\u9700\u8981\u5148\u77e5\u9053 Java \u5bf9\u8c61\u5934\u7684\u7ed3\u6784<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8ba4\u8bc6 Java \u5bf9\u8c61\u5934<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6309\u7167\u5e38\u89c4\u7406\u89e3\uff0c\u8bc6\u522b\u7ebf\u7a0b ID \u9700\u8981\u4e00\u7ec4 mapping \u6620\u5c04\u5173\u7cfb\u6765\u641e\u5b9a\uff0c\u5982\u679c\u5355\u72ec\u7ef4\u62a4\u8fd9\u4e2a mapping \u5173\u7cfb\u53c8\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002\u5965\u5361\u59c6\u5243\u5200\u539f\u7406\uff0cJava \u4e07\u7269\u7686\u662f\u5bf9\u8c61\uff0c\u5bf9\u8c61\u7686\u53ef\u7528\u4f5c\u9501\uff0c\u4e0e\u5176\u5355\u72ec\u7ef4\u62a4\u4e00\u4e2a mapping \u5173\u7cfb\uff0c\u4e0d\u5982\u4e2d\u5fc3\u5316\u5c06\u9501\u7684\u4fe1\u606f\u7ef4\u62a4\u5728 Java \u5bf9\u8c61\u672c\u8eab\u4e0a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u5bf9\u8c61\u5934\u6700\u591a\u7531\u4e09\u90e8\u5206\u6784\u6210\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>MarkWord<\/code><\/li><li>ClassMetadata Address<\/li><li>Array Length \uff08\u5982\u679c\u5bf9\u8c61\u662f\u6570\u7ec4\u624d\u4f1a\u6709\u8fd9\u90e8\u5206\uff09<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d<code>Markword<\/code>\u662f\u4fdd\u5b58\u9501\u72b6\u6001\u7684\u5173\u952e\uff0c\u5bf9\u8c61\u9501\u72b6\u6001\u53ef\u4ee5\u4ece\u504f\u5411\u9501\u5347\u7ea7\u5230\u8f7b\u91cf\u7ea7\u9501\uff0c\u518d\u5347\u7ea7\u5230\u91cd\u91cf\u7ea7\u9501\uff0c\u52a0\u4e0a\u521d\u59cb\u7684\u65e0\u9501\u72b6\u6001\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u6709 4 \u79cd\u72b6\u6001\u3002\u60f3\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u8868\u793a\u8fd9\u4e48\u591a\u4fe1\u606f\u81ea\u7136\u5c31\u8981\u7528<code>\u4f4d<\/code>\u5b58\u50a8\uff0c\u5728 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u662f\u8fd9\u6837\u5b58\u50a8\u7684\uff08\u6ce8\u610f\u989c\u8272\u6807\u8bb0\uff09\uff0c\u60f3\u770b\u5177\u4f53\u6ce8\u91ca\u7684\u53ef\u4ee5\u770b hotspot(1.8) \u6e90\u7801\u6587\u4ef6<code>path\/hotspot\/src\/share\/vm\/oops\/markOop.hpp<\/code>\u7b2c 30 \u884c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock4.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e86\u8fd9\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ea\u9700\u8981\u5f04\u6e05\u695a\uff0cMarkWord \u4e2d\u7684\u9501\u4fe1\u606f\u662f\u600e\u4e48\u53d8\u5316\u7684<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8ba4\u8bc6\u504f\u5411\u9501<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5355\u7eaf\u7684\u770b\u4e0a\u56fe\uff0c\u8fd8\u662f\u663e\u5f97\u5341\u5206\u62bd\u8c61\uff0c\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u7684\u6211\u4eec\u6700\u559c\u6b22\u7528\u4ee3\u7801\u8bf4\u8bdd\uff0c\u8d34\u5fc3\u7684 openjdk \u5b98\u7f51\u63d0\u4f9b\u4e86\u53ef\u4ee5\u67e5\u770b\u5bf9\u8c61\u5185\u5b58\u5e03\u5c40\u7684\u5de5\u5177 JOL (java object layout)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Maven Package<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n  &lt;groupId&gt;org.openjdk.jol&lt;\/groupId&gt;\n  &lt;artifactId&gt;jol-core&lt;\/artifactId&gt;\n  &lt;version&gt;0.14&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Gradle Package<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>implementation\u00a0'org.openjdk.jol:jol-core:0.14'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u901a\u8fc7\u4ee3\u7801\u6765\u6df1\u5165\u4e86\u89e3\u4e00\u4e0b\u504f\u5411\u9501\u5427<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u4e0a\u56fe(\u4ece\u5de6\u5230\u53f3) \u4ee3\u8868<code>\u9ad8\u4f4d -&gt; \u4f4e\u4f4d<\/code><\/p><p>JOL \u8f93\u51fa\u7ed3\u679c\uff08\u4ece\u5de6\u5230\u53f3\uff09\u4ee3\u8868<code>\u4f4e\u4f4d -&gt; \u9ad8\u4f4d<\/code>\u275e<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u770b\u6d4b\u8bd5\u4ee3\u7801<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String[] args) {\n  Object o = new Object();\n  log.info(\"\u672a\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n  synchronized (o){\n    log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n    log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u770b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock5.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u6211\u4eec\u7528\u5230\u7684 JOL \u7248\u672c\u4e3a<code>0.14<\/code>, \u5e26\u9886\u5927\u5bb6\u5feb\u901f\u4e86\u89e3\u4e00\u4e0b\u4f4d\u5177\u4f53\u503c\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u8981\u7528<code>0.16<\/code>\u7248\u672c\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u7248\u672c\u7ed9\u4e86\u6211\u4eec\u66f4\u53cb\u597d\u7684\u8bf4\u660e\uff0c\u540c\u6837\u7684\u4ee3\u7801\uff0c\u6765\u770b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock6.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u5230\u8fd9\u4e2a\u7ed3\u679c\uff0c\u4f60\u5e94\u8be5\u662f\u6709\u7591\u95ee\u7684\uff0cJDK 1.6 \u4e4b\u540e\u9ed8\u8ba4\u662f\u5f00\u542f\u504f\u5411\u9501\u7684\uff0c\u4e3a\u4ec0\u4e48\u521d\u59cb\u5316\u7684\u4ee3\u7801\u662f\u65e0\u9501\u72b6\u6001\uff0c\u8fdb\u5165\u540c\u6b65\u5757\u4ea7\u751f\u7ade\u4e89\u5c31\u7ed5\u8fc7\u504f\u5411\u9501\u76f4\u63a5\u53d8\u6210\u8f7b\u91cf\u7ea7\u9501\u4e86\u5462\uff1f<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u867d\u7136\u9ed8\u8ba4\u5f00\u542f\u4e86\u504f\u5411\u9501\uff0c\u4f46\u662f\u5f00\u542f\u6709\u5ef6\u8fdf\uff0c\u5927\u6982 4s\u3002\u539f\u56e0\u662f JVM \u5185\u90e8\u7684\u4ee3\u7801\u6709\u5f88\u591a\u5730\u65b9\u7528\u5230\u4e86synchronized\uff0c\u5982\u679c\u76f4\u63a5\u5f00\u542f\u504f\u5411\uff0c\u4ea7\u751f\u7ade\u4e89\u5c31\u8981\u6709\u9501\u5347\u7ea7\uff0c\u4f1a\u5e26\u6765\u989d\u5916\u7684\u6027\u80fd\u635f\u8017\uff0c\u6240\u4ee5\u5c31\u6709\u4e86\u5ef6\u8fdf\u7b56\u7565\u3002\u275e<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock7.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570<code>-XX:BiasedLockingStartupDelay=0<\/code>\u5c06\u5ef6\u8fdf\u6539\u4e3a0\uff0c\u4f46\u662f\u4e0d\u5efa\u8bae\u8fd9\u4e48\u505a\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u5f20\u56fe\u6765\u7406\u89e3\u4e00\u4e0b\u76ee\u524d\u7684\u60c5\u51b5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u6211\u4eec\u5c31\u4ee3\u7801\u5ef6\u8fdf 5 \u79d2\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u6765\u770b\u770b\u504f\u5411\u662f\u5426\u751f\u6548<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> public static void main(String[] args) throws InterruptedException {\n  \/\/ \u7761\u7720 5s\n  Thread.sleep(5000);\n  Object o = new Object();\n  log.info(\"\u672a\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n  synchronized (o){\n   log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u65b0\u67e5\u770b\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock8.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u7684\u7ed3\u679c\u662f\u7b26\u5408\u6211\u4eec\u9884\u671f\u7684\uff0c\u4f46\u662f\u7ed3\u679c\u4e2d\u7684<code>biasable<\/code>\u72b6\u6001\uff0c\u5728 MarkWord \u8868\u683c\u4e2d\u5e76\u4e0d\u5b58\u5728\uff0c\u5176\u5b9e\u8fd9\u662f\u4e00\u79cd\u533f\u540d\u504f\u5411\u72b6\u6001\uff0c\u662f\u5bf9\u8c61\u521d\u59cb\u5316\u4e2d\uff0cJVM \u5e2e\u6211\u4eec\u505a\u7684<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u5f53\u6709\u7ebf\u7a0b\u8fdb\u5165\u540c\u6b65\u5757\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u53ef\u504f\u5411\u72b6\u6001\uff1a\u76f4\u63a5\u5c31 CAS \u66ff\u6362 ThreadID\uff0c\u5982\u679c\u6210\u529f\uff0c\u5c31\u53ef\u4ee5\u83b7\u53d6\u504f\u5411\u9501\u4e86<\/li><li>\u4e0d\u53ef\u504f\u5411\u72b6\u6001\uff1a\u5c31\u4f1a\u53d8\u6210\u8f7b\u91cf\u7ea7\u9501<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u95ee\u9898\u53c8\u6765\u4e86\uff0c\u73b0\u5728\u9501\u5bf9\u8c61\u6709\u5177\u4f53\u504f\u5411\u7684\u7ebf\u7a0b\uff0c\u5982\u679c\u65b0\u7684\u7ebf\u7a0b\u8fc7\u6765\u6267\u884c\u540c\u6b65\u5757\u4f1a\u504f\u5411\u65b0\u7684\u7ebf\u7a0b\u5417\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String[] args) throws InterruptedException {\n  \/\/ \u7761\u7720 5s\n  Thread.sleep(5000);\n  Object o = new Object();\n  log.info(\"\u672a\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n  synchronized (o){\n   log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n\n  Thread t2 = new Thread(() -&gt; {\n   synchronized (o) {\n    log.info(\"\u65b0\u7ebf\u7a0b\u83b7\u53d6\u9501\uff0cMarkWord\u4e3a\uff1a\");\n    log.info(ClassLayout.parseInstance(o).toPrintable());\n   }\n  });\n\n  t2.start();\n  t2.join();\n  log.info(\"\u4e3b\u7ebf\u7a0b\u518d\u6b21\u67e5\u770b\u9501\u5bf9\u8c61\uff0cMarkWord\u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n\n  synchronized (o){\n   log.info((\"\u4e3b\u7ebf\u7a0b\u518d\u6b21\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u770b\u8fd0\u884c\u7ed3\u679c\uff0c\u5947\u602a\u7684\u4e8b\u60c5\u53d1\u751f\u4e86\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock9.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><code>\u6807\u8bb01<\/code>: \u521d\u59cb\u53ef\u504f\u5411\u72b6\u6001<\/li><li><code>\u6807\u8bb02<\/code>\uff1a\u504f\u5411\u4e3b\u7ebf\u7a0b\u540e\uff0c\u4e3b\u7ebf\u7a0b\u9000\u51fa\u540c\u6b65\u4ee3\u7801\u5757<\/li><li><code>\u6807\u8bb03<\/code>: &nbsp;\u65b0\u7ebf\u7a0b\u8fdb\u5165\u540c\u6b65\u4ee3\u7801\u5757\uff0c\u5347\u7ea7\u6210\u4e86\u8f7b\u91cf\u7ea7\u9501<\/li><li><code>\u6807\u8bb04<\/code>: \u65b0\u7ebf\u7a0b\u8f7b\u91cf\u7ea7\u9501\u9000\u51fa\u540c\u6b65\u4ee3\u7801\u5757\uff0c\u4e3b\u7ebf\u7a0b\u67e5\u770b\uff0c\u53d8\u4e3a\u4e0d\u53ef\u504f\u5411\u72b6\u6001<\/li><li><code>\u6807\u8bb05<\/code>: \u7531\u4e8e\u5bf9\u8c61\u4e0d\u53ef\u504f\u5411\uff0c\u540c\u573a\u666f1\u4e3b\u7ebf\u7a0b\u518d\u6b21\u8fdb\u5165\u540c\u6b65\u5757\uff0c\u81ea\u7136\u5c31\u4f1a\u7528\u8f7b\u91cf\u7ea7\u9501<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u6b64\uff0c\u573a\u666f\u4e00\u4e8c\u4e09\u53ef\u4ee5\u603b\u7ed3\u4e3a\u4e00\u5f20\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock10.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8fd9\u6837\u7684\u8fd0\u884c\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u504f\u5411\u9501\u50cf\u662f\u201c\u4e00\u9524\u5b50\u4e70\u5356\u201d\uff0c\u53ea\u8981\u504f\u5411\u4e86\u67d0\u4e2a\u7ebf\u7a0b\uff0c\u540e\u7eed\u5176\u4ed6\u7ebf\u7a0b\u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u90fd\u4f1a\u53d8\u4e3a\u8f7b\u91cf\u7ea7\u9501\uff0c\u8fd9\u6837\u7684\u504f\u5411\u975e\u5e38\u6709\u5c40\u9650\u6027\u3002\u4e8b\u5b9e\u4e0a\u5e76\u4e0d\u662f\u8fd9\u6837\uff0c\u5982\u679c\u4f60\u4ed4\u7ec6\u770b\u6807\u8bb02\uff08\u5df2\u504f\u5411\u72b6\u6001\uff09\uff0c\u8fd8\u6709\u4e2a epoch \u6211\u4eec\u6ca1\u6709\u63d0\u53ca\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f\u6253\u7834\u8fd9\u79cd\u5c40\u9650\u6027\u7684\u5173\u952e\uff0c\u5728\u4e86\u89e3 epoch \u4e4b\u524d\uff0c\u6211\u4eec\u8fd8\u8981\u4e86\u89e3\u4e00\u4e2a\u6982\u5ff5\u2014\u2014\u504f\u5411\u64a4\u9500<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u504f\u5411\u64a4\u9500<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u771f\u6b63\u8bb2\u89e3\u504f\u5411\u64a4\u9500\u4e4b\u524d\uff0c\u9700\u8981\u548c\u5927\u5bb6\u660e\u786e\u4e00\u4e2a\u6982\u5ff5\u2014\u2014\u504f\u5411\u9501\u64a4\u9500\u548c\u504f\u5411\u9501\u91ca\u653e\u662f\u4e24\u7801\u4e8b<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u64a4\u9500\uff1a\u7b3c\u7edf\u7684\u8bf4\u5c31\u662f\u591a\u4e2a\u7ebf\u7a0b\u7ade\u4e89\u5bfc\u81f4\u4e0d\u80fd\u518d\u4f7f\u7528\u504f\u5411\u6a21\u5f0f\u7684\u65f6\u5019\uff0c\u4e3b\u8981\u662f\u544a\u77e5\u8fd9\u4e2a\u9501\u5bf9\u8c61\u4e0d\u80fd\u518d\u7528\u504f\u5411\u6a21\u5f0f<\/li><li>\u91ca\u653e\uff1a\u548c\u4f60\u7684\u5e38\u89c4\u7406\u89e3\u4e00\u6837\uff0c\u5bf9\u5e94\u7684\u5c31\u662f synchronized \u65b9\u6cd5\u7684\u9000\u51fa\u6216 synchronized \u5757\u7684\u7ed3\u675f<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f55\u4e3a\u504f\u5411\u64a4\u9500\uff1f<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u4ece\u504f\u5411\u72b6\u6001\u64a4\u56de\u539f\u6709\u7684\u72b6\u6001\uff0c\u4e5f\u5c31\u662f\u5c06 MarkWord \u7684\u7b2c 3 \u4f4d\uff08\u662f\u5426\u504f\u5411\u64a4\u9500\uff09\u7684\u503c\uff0c<code>\u4ece 1 \u53d8\u56de 0<\/code>\u275e<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53ea\u662f\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u9501\uff0c\u518d\u52a0\u4e0a\u300c\u504f\u5fc3\u300d\u7684\u673a\u5236\uff0c\u662f\u6ca1\u6709\u7406\u7531\u64a4\u9500\u504f\u5411\u7684\uff0c\u6240\u4ee5\u504f\u5411\u7684\u64a4\u9500\u53ea\u80fd\u53d1\u751f\u5728\u6709\u7ade\u4e89\u7684\u60c5\u51b5\u4e0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u60f3\u8981\u64a4\u9500\u504f\u5411\u9501\uff0c\u8fd8\u4e0d\u80fd\u5bf9\u6301\u6709\u504f\u5411\u9501\u7684\u7ebf\u7a0b\u6709\u5f71\u54cd\uff0c\u6240\u4ee5\u5c31\u8981\u7b49\u5f85\u6301\u6709\u504f\u5411\u9501\u7684\u7ebf\u7a0b\u5230\u8fbe\u4e00\u4e2a<code>safepoint \u5b89\u5168\u70b9<\/code>(\u8fd9\u91cc\u7684\u5b89\u5168\u70b9\u662f JVM \u4e3a\u4e86\u4fdd\u8bc1\u5728\u5783\u573e\u56de\u6536\u7684\u8fc7\u7a0b\u4e2d\u5f15\u7528\u5173\u7cfb\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\u8bbe\u7f6e\u7684\u4e00\u79cd\u5b89\u5168\u72b6\u6001\uff0c\u5728\u8fd9\u4e2a\u72b6\u6001\u4e0a\u4f1a\u6682\u505c\u6240\u6709\u7ebf\u7a0b\u5de5\u4f5c)\uff0c \u5728\u8fd9\u4e2a\u5b89\u5168\u70b9\u4f1a\u6302\u8d77\u83b7\u5f97\u504f\u5411\u9501\u7684\u7ebf\u7a0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u5b89\u5168\u70b9\uff0c\u7ebf\u7a0b\u53ef\u80fd\u8fd8\u662f\u5904\u5728\u4e0d\u540c\u72b6\u6001\u7684\uff0c\u5148\u8bf4\u7ed3\u8bba\uff08\u56e0\u4e3a\u6e90\u7801\u5c31\u662f\u8fd9\u4e48\u5199\u7684\uff0c\u53ef\u80fd\u6709\u7591\u60d1\u7684\u5730\u65b9\u4f1a\u5728\u540e\u9762\u89e3\u91ca\uff09<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u7ebf\u7a0b\u4e0d\u5b58\u6d3b\u6216\u8005\u6d3b\u7740\u7684\u7ebf\u7a0b\u4f46\u9000\u51fa\u4e86\u540c\u6b65\u5757\uff0c\u5f88\u7b80\u5355\uff0c\u76f4\u63a5\u64a4\u9500\u504f\u5411\u5c31\u597d\u4e86<\/li><li>\u6d3b\u7740\u7684\u7ebf\u7a0b\u4f46\u4ecd\u5728\u540c\u6b65\u5757\u4e4b\u5185\uff0c\u90a3\u5c31\u8981\u5347\u7ea7\u6210\u8f7b\u91cf\u7ea7\u9501<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u548c epoch \u8c8c\u4f3c\u8fd8\u662f\u6ca1\u5565\u5173\u7cfb\uff0c\u56e0\u4e3a\u8fd9\u8fd8\u4e0d\u662f\u5168\u90e8\u573a\u666f\u3002\u504f\u5411\u9501\u662f\u7279\u5b9a\u573a\u666f\u4e0b\u63d0\u5347\u7a0b\u5e8f\u6548\u7387\u7684\u65b9\u6848\uff0c\u53ef\u5e76\u4e0d\u4ee3\u8868\u7a0b\u5e8f\u5458\u5199\u7684\u7a0b\u5e8f\u90fd\u6ee1\u8db3\u8fd9\u4e9b\u7279\u5b9a\u573a\u666f\uff0c\u6bd4\u5982\u8fd9\u4e9b\u573a\u666f\uff08\u5728\u5f00\u542f\u504f\u5411\u9501\u7684\u524d\u63d0\u4e0b\uff09\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u4e00\u4e2a\u7ebf\u7a0b\u521b\u5efa\u4e86\u5927\u91cf\u5bf9\u8c61\u5e76\u6267\u884c\u4e86\u521d\u59cb\u7684\u540c\u6b65\u64cd\u4f5c\uff0c\u4e4b\u540e\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u4f5c\u4e3a\u9501\u8fdb\u884c\u4e4b\u540e\u7684\u64cd\u4f5c\u3002\u8fd9\u79cdcase\u4e0b\uff0c\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u504f\u5411\u9501\u64a4\u9500\u64cd\u4f5c<\/li><li>\u660e\u77e5\u6709\u591a\u7ebf\u7a0b\u7ade\u4e89\uff08\u751f\u4ea7\u8005\/\u6d88\u8d39\u8005\u961f\u5217\uff09\uff0c\u8fd8\u8981\u4f7f\u7528\u504f\u5411\u9501\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u5404\u79cd\u64a4\u9500<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f88\u663e\u7136\uff0c\u8fd9\u4e24\u79cd\u573a\u666f\u80af\u5b9a\u4f1a\u5bfc\u81f4\u504f\u5411\u64a4\u9500\u7684\uff0c\u4e00\u4e2a\u504f\u5411\u64a4\u9500\u7684\u6210\u672c\u65e0\u6240\u8c13\uff0c\u5927\u91cf\u504f\u5411\u64a4\u9500\u7684\u6210\u672c\u662f\u4e0d\u80fd\u5ffd\u89c6\u7684\u3002\u90a3\u600e\u4e48\u529e\uff1f\u65e2\u4e0d\u60f3\u7981\u7528\u504f\u5411\u9501\uff0c\u8fd8\u4e0d\u60f3\u5fcd\u53d7\u5927\u91cf\u64a4\u9500\u504f\u5411\u589e\u52a0\u7684\u6210\u672c\uff0c\u8fd9\u79cd\u65b9\u6848\u5c31\u662f\u8bbe\u8ba1\u4e00\u4e2a\u6709\u9636\u68af\u7684\u5e95\u7ebf<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6279\u91cf\u91cd\u504f\u5411\uff08bulk rebias\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u7b2c\u4e00\u79cd\u573a\u666f\u7684\u5feb\u901f\u89e3\u51b3\u65b9\u6848\uff0c\u4ee5 class \u4e3a\u5355\u4f4d\uff0c\u4e3a\u6bcf\u4e2a class \u7ef4\u62a4\u4e00\u4e2a\u504f\u5411\u9501\u64a4\u9500\u8ba1\u6570\u5668\uff0c\u6bcf\u4e00\u6b21\u8be5class\u7684\u5bf9\u8c61\u53d1\u751f\u504f\u5411\u64a4\u9500\u64cd\u4f5c\u65f6\uff0c\u8be5\u8ba1\u6570\u5668<code>+1<\/code>\uff0c\u5f53\u8fd9\u4e2a\u503c\u8fbe\u5230\u91cd\u504f\u5411\u9608\u503c\uff08\u9ed8\u8ba420\uff09\u65f6\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>BiasedLockingBulkRebiasThreshold&nbsp;=&nbsp;20   <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JVM \u5c31\u8ba4\u4e3a\u8be5class\u7684\u504f\u5411\u9501\u6709\u95ee\u9898\uff0c\u56e0\u6b64\u4f1a\u8fdb\u884c\u6279\u91cf\u91cd\u504f\u5411, \u5b83\u7684\u5b9e\u73b0\u65b9\u5f0f\u5c31\u7528\u5230\u4e86\u6211\u4eec\u4e0a\u9762\u8bf4\u7684<code>epoch<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Epoch<\/code>\uff0c\u5982\u5176\u542b\u4e49\u300c\u7eaa\u5143\u300d\u4e00\u6837\uff0c\u5c31\u662f\u4e00\u4e2a\u65f6\u95f4\u6233\u3002\u6bcf\u4e2a class \u5bf9\u8c61\u4f1a\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684<code>epoch<\/code>\u5b57\u6bb5\uff0c\u6bcf\u4e2a\u5904\u4e8e\u504f\u5411\u9501\u72b6\u6001\u5bf9\u8c61\u7684<code>mark word<\/code>\u4e2d\u4e5f\u6709\u8be5\u5b57\u6bb5\uff0c\u5176\u521d\u59cb\u503c\u4e3a\u521b\u5efa\u8be5\u5bf9\u8c61\u65f6 class \u4e2d\u7684<code>epoch<\/code>\u7684\u503c\uff08\u6b64\u65f6\u4e8c\u8005\u662f\u76f8\u7b49\u7684\uff09\u3002\u6bcf\u6b21\u53d1\u751f\u6279\u91cf\u91cd\u504f\u5411\u65f6\uff0c\u5c31\u5c06\u8be5\u503c\u52a01\uff0c\u540c\u65f6\u904d\u5386JVM\u4e2d\u6240\u6709\u7ebf\u7a0b\u7684\u6808<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u627e\u5230\u8be5 class \u6240\u6709\u6b63\u5904\u4e8e\u52a0\u9501\u72b6\u6001\u7684\u504f\u5411\u9501\u5bf9\u8c61\uff0c\u5c06\u5176<code>epoch<\/code>\u5b57\u6bb5\u6539\u4e3a\u65b0\u503c<\/li><li>class \u4e2d\u4e0d\u5904\u4e8e\u52a0\u9501\u72b6\u6001\u7684\u504f\u5411\u9501\u5bf9\u8c61\uff08\u6ca1\u88ab\u4efb\u4f55\u7ebf\u7a0b\u6301\u6709\uff0c\u4f46\u4e4b\u524d\u662f\u88ab\u7ebf\u7a0b\u6301\u6709\u8fc7\u7684\uff0c\u8fd9\u79cd\u9501\u5bf9\u8c61\u7684 markword \u80af\u5b9a\u4e5f\u662f\u6709\u504f\u5411\u7684\uff09\uff0c\u4fdd\u6301<code>epoch<\/code>\u5b57\u6bb5\u503c\u4e0d\u53d8<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u4e0b\u6b21\u83b7\u5f97\u9501\u65f6\uff0c\u53d1\u73b0\u5f53\u524d\u5bf9\u8c61\u7684<code>epoch<\/code>\u503c\u548cclass\u7684<code>epoch<\/code>\uff0c\u672c\u7740\u4eca\u671d\u4e0d\u95ee\u524d\u671d\u4e8b\u7684\u539f\u5219\uff08\u4e0a\u4e00\u4e2a\u7eaa\u5143\uff09\uff0c\u90a3\u5c31\u7b97\u5f53\u524d\u5df2\u7ecf\u504f\u5411\u4e86\u5176\u4ed6\u7ebf\u7a0b\uff0c\u4e5f\u4e0d\u4f1a\u6267\u884c\u64a4\u9500\u64cd\u4f5c\uff0c\u800c\u662f\u76f4\u63a5\u901a\u8fc7 CAS \u64cd\u4f5c\u5c06\u5176<code>mark word<\/code>\u7684\u7ebf\u7a0b ID \u6539\u6210\u5f53\u524d\u7ebf\u7a0b ID\uff0c\u8fd9\u4e5f\u7b97\u662f\u4e00\u5b9a\u7a0b\u5ea6\u7684\u4f18\u5316\uff0c\u6bd5\u7adf\u6ca1\u5347\u7ea7\u9501\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c<code>epoch<\/code>\u90fd\u4e00\u6837\uff0c\u8bf4\u660e\u6ca1\u6709\u53d1\u751f\u8fc7\u6279\u91cf\u91cd\u504f\u5411, \u5982\u679c<code>markword<\/code>\u6709\u7ebf\u7a0bID\uff0c\u8fd8\u6709\u5176\u4ed6\u9501\u6765\u7ade\u4e89\uff0c\u90a3\u9501\u81ea\u7136\u662f\u8981\u5347\u7ea7\u7684(\u5982\u540c\u524d\u9762\u4e3e\u7684\u4f8b\u5b50 epoch=0)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6279\u91cf\u91cd\u504f\u5411\u662f\u7b2c\u4e00\u9636\u68af\u5e95\u7ebf\uff0c\u8fd8\u6709\u7b2c\u4e8c\u9636\u68af\u5e95\u7ebf<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6279\u91cf\u64a4\u9500\uff08bulk revoke\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u8fbe\u5230\u91cd\u504f\u5411\u9608\u503c\u540e\uff0c\u5047\u8bbe\u8be5 class \u8ba1\u6570\u5668\u7ee7\u7eed\u589e\u957f\uff0c\u5f53\u5176\u8fbe\u5230\u6279\u91cf\u64a4\u9500\u7684\u9608\u503c\u540e\uff08\u9ed8\u8ba440\uff09\u65f6\uff0c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>BiasedLockingBulkRevokeThreshold&nbsp;=&nbsp;40   <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JVM\u5c31\u8ba4\u4e3a\u8be5 class \u7684\u4f7f\u7528\u573a\u666f\u5b58\u5728\u591a\u7ebf\u7a0b\u7ade\u4e89\uff0c\u4f1a\u6807\u8bb0\u8be5 class \u4e3a\u4e0d\u53ef\u504f\u5411\u3002\u4e4b\u540e\u5bf9\u4e8e\u8be5 class \u7684\u9501\uff0c\u76f4\u63a5\u8d70\u8f7b\u91cf\u7ea7\u9501\u7684\u903b\u8f91<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\u7b2c\u4e8c\u9636\u68af\u5e95\u7ebf\uff0c\u4f46\u662f\u5728\u7b2c\u4e00\u9636\u68af\u5230\u7b2c\u4e8c\u9636\u68af\u7684\u8fc7\u6e21\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u5c31\u662f\u5728\u5f7b\u5e95\u7981\u7528\u504f\u5411\u9501\u4e4b\u524d\uff0c\u8fd8\u7ed9\u4e00\u6b21\u6539\u8fc7\u81ea\u65b0\u7684\u673a\u4f1a\uff0c\u90a3\u5c31\u662f\u53e6\u5916\u4e00\u4e2a\u8ba1\u65f6\u5668\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>BiasedLockingDecayTime&nbsp;=&nbsp;25000   <\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5982\u679c\u5728\u8ddd\u79bb\u4e0a\u6b21\u6279\u91cf\u91cd\u504f\u5411\u53d1\u751f\u7684 25 \u79d2\u4e4b\u5185\uff0c\u5e76\u4e14\u7d2f\u8ba1\u64a4\u9500\u8ba1\u6570\u8fbe\u523040\uff0c\u5c31\u4f1a\u53d1\u751f\u6279\u91cf\u64a4\u9500\uff08\u504f\u5411\u9501\u5f7b\u5e95 game over\uff09\n<\/li><li>\u5982\u679c\u5728\u8ddd\u79bb\u4e0a\u6b21\u6279\u91cf\u91cd\u504f\u5411\u53d1\u751f\u8d85\u8fc7 25 \u79d2\u4e4b\u5916\uff0c\u90a3\u4e48\u5c31\u4f1a\u91cd\u7f6e\u5728<code>[20, 40)<\/code>\u5185\u7684\u8ba1\u6570, \u518d\u7ed9\u6b21\u673a\u4f1a\n<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u5bb6\u6709\u5174\u8da3\u53ef\u4ee5\u5199\u4ee3\u7801\u6d4b\u8bd5\u4e00\u4e0b\u4e34\u754c\u70b9\uff0c\u89c2\u5bdf\u9501\u5bf9\u8c61<code>markword<\/code>\u7684\u53d8\u5316<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u6b64\uff0c\u6574\u4e2a\u504f\u5411\u9501\u7684\u5de5\u4f5c\u6d41\u7a0b\u53ef\u4ee5\u7528\u4e00\u5f20\u56fe\u8868\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock11.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u6b64\uff0c\u4f60\u5e94\u8be5\u5bf9\u504f\u5411\u9501\u6709\u4e2a\u57fa\u672c\u7684\u8ba4\u8bc6\u4e86\uff0c\u4f46\u662f\u6211\u5fc3\u4e2d\u7684\u597d\u591a\u7591\u95ee\u8fd8\u6ca1\u6709\u89e3\u9664\uff0c\u54b1\u4eec\u7ee7\u7eed\u770b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HashCode \u54ea\u53bb\u4e86<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u573a\u666f\u4e00\uff0c\u65e0\u9501\u72b6\u6001\uff0c\u5bf9\u8c61\u5934\u4e2d\u6ca1\u6709 hashcode\uff1b\u504f\u5411\u9501\u72b6\u6001\uff0c\u5bf9\u8c61\u5934\u8fd8\u662f\u6ca1\u6709 hashcode\uff0c\u90a3\u6211\u4eec\u7684 hashcode \u54ea\u53bb\u4e86\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u8981\u77e5\u9053\uff0chashcode \u4e0d\u662f\u521b\u5efa\u5bf9\u8c61\u5c31\u5e2e\u6211\u4eec\u5199\u5230\u5bf9\u8c61\u5934\u4e2d\u7684\uff0c\u800c\u662f\u8981\u7ecf\u8fc7\u7b2c\u4e00\u6b21\u8c03\u7528<code>Object::hashCode()<\/code>\u6216\u8005<code>System::identityHashCode(Object)<\/code>\u624d\u4f1a\u5b58\u50a8\u5728\u5bf9\u8c61\u5934\u4e2d\u7684\u3002\u7b2c\u4e00\u6b21\u751f\u6210\u7684 hashcode\u540e\uff0c\u8be5\u503c\u5e94\u8be5\u662f\u4e00\u76f4\u4fdd\u6301\u4e0d\u53d8\u7684\uff0c\u4f46\u504f\u5411\u9501\u53c8\u662f\u6765\u56de\u66f4\u6539\u9501\u5bf9\u8c61\u7684 markword\uff0c\u5fc5\u5b9a\u4f1a\u5bf9 hashcode \u7684\u751f\u6210\u6709\u5f71\u54cd\uff0c\u90a3\u600e\u4e48\u529e\u5462\uff1f\uff0c\u6211\u4eec\u6765\u7528\u4ee3\u7801\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u573a\u666f\u4e00<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String[] args) throws InterruptedException {\n  \/\/ \u7761\u7720 5s\n  Thread.sleep(5000);\n\n  Object o = new Object();\n  log.info(\"\u672a\u751f\u6210 hashcode\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n\n  o.hashCode();\n  log.info(\"\u5df2\u751f\u6210 hashcode\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n\n  synchronized (o){\n   log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u770b\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock12.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u7ed3\u8bba\u5c31\u662f\uff1a\u5373\u4fbf\u521d\u59cb\u5316\u4e3a\u53ef\u504f\u5411\u72b6\u6001\u7684\u5bf9\u8c61\uff0c\u4e00\u65e6\u8c03\u7528<code>Object::hashCode()<\/code>\u6216\u8005<code>System::identityHashCode(Object)<\/code>\uff0c\u8fdb\u5165\u540c\u6b65\u5757\u5c31\u4f1a\u76f4\u63a5\u4f7f\u7528\u8f7b\u91cf\u7ea7\u9501\u275e<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">\u573a\u666f\u4e8c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u5982\u5df2\u504f\u5411\u67d0\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u7136\u540e\u751f\u6210 hashcode\uff0c\u7136\u540e\u540c\u4e00\u4e2a\u7ebf\u7a0b\u53c8\u8fdb\u5165\u540c\u6b65\u5757\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u6765\u770b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String[] args) throws InterruptedException {\n  \/\/ \u7761\u7720 5s\n  Thread.sleep(5000);\n\n  Object o = new Object();\n  log.info(\"\u672a\u751f\u6210 hashcode\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n\n  synchronized (o){\n   log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n\n  o.hashCode();\n  log.info(\"\u751f\u6210 hashcode\");\n  synchronized (o){\n   log.info((\"\u540c\u4e00\u7ebf\u7a0b\u518d\u6b21\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u770b\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock13.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u7ed3\u8bba\u5c31\u662f\uff1a\u540c\u573a\u666f\u4e00\uff0c\u4f1a\u76f4\u63a5\u4f7f\u7528\u8f7b\u91cf\u7ea7\u9501\u275e<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">\u573a\u666f\u4e09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u5047\u5982\u5bf9\u8c61\u5904\u4e8e\u5df2\u504f\u5411\u72b6\u6001\uff0c\u5728\u540c\u6b65\u5757\u4e2d\u8c03\u7528\u4e86\u90a3\u4e24\u4e2a\u65b9\u6cd5\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u7ee7\u7eed\u4ee3\u7801\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String[] args) throws InterruptedException {\n  \/\/ \u7761\u7720 5s\n  Thread.sleep(5000);\n\n  Object o = new Object();\n  log.info(\"\u672a\u751f\u6210 hashcode\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n\n  synchronized (o){\n   log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n   o.hashCode();\n   log.info(\"\u5df2\u504f\u5411\u72b6\u6001\u4e0b\uff0c\u751f\u6210 hashcode\uff0cMarkWord \u4e3a\uff1a\");\n   log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u770b\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock14.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u7ed3\u8bba\u5c31\u662f\uff1a\u5982\u679c\u5bf9\u8c61\u5904\u5728\u5df2\u504f\u5411\u72b6\u6001\uff0c\u751f\u6210 hashcode \u540e\uff0c\u5c31\u4f1a\u76f4\u63a5\u5347\u7ea7\u6210\u91cd\u91cf\u7ea7\u9501\u275e<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u7528\u4e66\u4e2d\u7684\u4e00\u6bb5\u8bdd\u6765\u63cf\u8ff0 \u9501\u548c hashcode \u4e4b\u524d\u7684\u5173\u7cfb<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock15.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u8c03\u7528 Object.wait() \u65b9\u6cd5\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Object \u9664\u4e86\u63d0\u4f9b\u4e86\u4e0a\u8ff0 hashcode \u65b9\u6cd5\uff0c\u8fd8\u6709<code>wait()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e5f\u662f\u6211\u4eec\u5728\u540c\u6b65\u5757\u4e2d\u5e38\u7528\u7684\uff0c\u90a3\u8fd9\u4f1a\u5bf9\u9501\u4ea7\u751f\u54ea\u4e9b\u5f71\u54cd\u5462\uff1f\u6765\u770b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String[] args) throws InterruptedException {\n  \/\/ \u7761\u7720 5s\n  Thread.sleep(5000);\n\n  Object o = new Object();\n  log.info(\"\u672a\u751f\u6210 hashcode\uff0cMarkWord \u4e3a\uff1a\");\n  log.info(ClassLayout.parseInstance(o).toPrintable());\n\n  synchronized (o) {\n    log.info((\"\u8fdb\u5165\u540c\u6b65\u5757\uff0cMarkWord \u4e3a\uff1a\"));\n    log.info(ClassLayout.parseInstance(o).toPrintable());\n\n    log.info(\"wait 2s\");\n    o.wait(2000);\n\n    log.info((\"\u8c03\u7528 wait \u540e\uff0cMarkWord \u4e3a\uff1a\"));\n    log.info(ClassLayout.parseInstance(o).toPrintable());\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u770b\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock16.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>\u7ed3\u8bba\u5c31\u662f\uff0cwait \u65b9\u6cd5\u662f\u4e92\u65a5\u91cf\uff08\u91cd\u91cf\u7ea7\u9501\uff09\u72ec\u6709\u7684\uff0c\u4e00\u65e6\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u5c31\u4f1a\u5347\u7ea7\u6210\u91cd\u91cf\u7ea7\u9501\uff08\u8fd9\u4e2a\u662f\u9762\u8bd5\u53ef\u4ee5\u8bf4\u51fa\u7684\u4eae\u70b9\u5185\u5bb9\u54e6\uff09\u275e<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u518d\u7ee7\u7eed\u4e30\u5bcc\u4e00\u4e0b\u9501\u5bf9\u8c61\u53d8\u5316\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock17.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u544a\u522b\u504f\u5411\u9501<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u5230\u8fd9\u4e2a\u6807\u9898\u4f60\u5e94\u8be5\u662f\u6709\u4e9b\u614c\uff0c\u4e3a\u5565\u8981\u544a\u522b\u504f\u5411\u9501\uff0c\u56e0\u4e3a\u7ef4\u62a4\u6210\u672c\u6709\u4e9b\u9ad8\u4e86\uff0c\u6765\u770b Open JDK \u5b98\u65b9\u58f0\u660e\uff0cJEP 374: Deprecate and Disable Biased Locking\uff0c\u76f8\u4fe1\u4f60\u770b\u4e0a\u9762\u7684\u6587\u5b57\u8bf4\u660e\u4e5f\u6df1\u6709\u4f53\u4f1a\uff0c\u4e3a\u4e86\u4e00\u4e2a\u73b0\u5728\u5c11\u6709\u7684\u573a\u666f\u4ed8\u51fa\u4e86\u5de8\u5927\u7684\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock18.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u8bf4\u660e\u7684\u66f4\u65b0\u65f6\u95f4\u8ddd\u79bb\u73b0\u5728\u5f88\u8fd1\uff0c\u5728 JDK15 \u7248\u672c\u5c31\u5df2\u7ecf\u5f00\u59cb\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock19.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u53e5\u8bdd\u89e3\u91ca\u5c31\u662f\u7ef4\u62a4\u6210\u672c\u592a\u9ad8\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock20.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock21.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u7ec8\u5c31\u662f\uff0cJDK 15 \u4e4b\u524d\uff0c\u504f\u5411\u9501\u9ed8\u8ba4\u662f enabled\uff0c\u4ece 15 \u5f00\u59cb\uff0c\u9ed8\u8ba4\u5c31\u662f disabled\uff0c\u9664\u975e\u663e\u793a\u7684\u901a\u8fc7<code>UseBiasedLocking \u5f00\u542f<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d\u5728 quarkus \u4e0a\u7684\u4e00\u7bc7\u6587\u7ae0\u8bf4\u660e\u7684\u66f4\u52a0\u76f4\u63a5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/08\/lock22.png\" alt=\"\" \/><figcaption>\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u504f\u5411\u9501\u7ed9 JVM \u589e\u52a0\u4e86\u5de8\u5927\u7684\u590d\u6742\u6027\uff0c\u53ea\u6709\u5c11\u6570\u975e\u5e38\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u624d\u80fd\u7406\u89e3\u6574\u4e2a\u8fc7\u7a0b\uff0c\u7ef4\u62a4\u6210\u672c\u5f88\u9ad8\uff0c\u5927\u5927\u963b\u788d\u4e86\u5f00\u53d1\u65b0\u7279\u6027\u7684\u8fdb\u7a0b\uff08\u6362\u4e2a\u89d2\u5ea6\u7406\u89e3\uff0c\u4f60\u638c\u63e1\u4e86\uff0c\u662f\u4e0d\u662f\u5c31\u662f\u90a3\u5c11\u6570\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u4e86\u5462\uff1f\u54c8\u54c8\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u504f\u5411\u9501\u53ef\u80fd\u5c31\u8fd9\u6837\u7684\u8d70\u5b8c\u4e86\u5b83\u7684\u4e00\u751f\uff0c\u6709\u4e9b\u540c\u5b66\u53ef\u80fd\u76f4\u63a5\u53d1\u95ee\uff0c\u90fd\u88ab deprecated \u4e86\uff0cJDK\u90fd 17 \u4e86\uff0c\u8fd8\u8bb2\u8fd9\u4e48\u591a\u5e72\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>java \u4efb\u5b83\u53d1\uff0c\u6211\u7528 Java8\uff0c\u8fd9\u662f\u5f88\u591a\u4e3b\u6d41\u7684\u72b6\u6001\uff0c\u81f3\u5c11\u4f60\u7528\u7684\u7248\u672c\u6ca1\u6709\u88ab deprecated<\/li><li>\u9762\u8bd5\u8fd8\u662f\u4f1a\u88ab\u7ecf\u5e38\u95ee\u5230<\/li><li>\u4e07\u4e00\u54ea\u5929\u6709\u66f4\u597d\u7684\u8bbe\u8ba1\u65b9\u6848\uff0c\u201c\u504f\u5411\u9501\u201d\u53c8\u4ee5\u65b0\u7684\u5f62\u5f0f\u56de\u6765\u4e86\u5462\uff0c\u4e86\u89e3\u53d8\u5316\u624d\u80fd\u66f4\u597d\u7406\u89e3\u80cc\u540e\u8bbe\u8ba1<\/li><li>\u5965\u5361\u59c6\u5243\u5200\u539f\u7406\uff0c\u6211\u4eec\u73b0\u5b9e\u4e2d\u7684\u4f18\u5316\u4e5f\u4e00\u6837\uff0c\u5982\u679c\u6ca1\u6709\u5fc5\u8981\u4e0d\u8981\u589e\u52a0\u5b9e\u4f53\uff0c\u5982\u679c\u589e\u52a0\u7684\u5185\u5bb9\u5e26\u6765\u5f88\u5927\u7684\u6210\u672c\uff0c\u4e0d\u5982\u5927\u80c6\u7684\u5e9f\u9664\u6389\uff0c\u63a5\u53d7\u4e00\u70b9\u843d\u5dee<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e4b\u524d\u5bf9\u4e8e\u504f\u5411\u9501\u6211\u4e5f\u53ea\u662f\u5355\u7eaf\u7684\u7406\u8bba\u8ba4\u77e5\uff0c\u4f46\u662f\u4e3a\u4e86\u5199\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u7ffb\u9605\u4e86\u5f88\u591a\u8d44\u6599\uff0c\u5305\u62ec\u4e5f\u91cd\u65b0\u67e5\u770b Hotspot \u6e90\u7801\uff0c\u8bf4\u7684\u8fd9\u4e9b\u5185\u5bb9\u4e5f\u5e76\u4e0d\u80fd\u5b8c\u5168\u8bf4\u660e\u504f\u5411\u9501\u7684\u6574\u4e2a\u6d41\u7a0b\u7ec6\u8282\uff0c\u8fd8\u9700\u8981\u5927\u5bb6\u5177\u4f53\u5b9e\u8df5\u8ffd\u8e2a\u67e5\u770b\uff0c\u8fd9\u91cc\u7ed9\u51fa\u6e90\u7801\u7684\u51e0\u4e2a\u5173\u952e\u5165\u53e3\uff0c\u65b9\u4fbf\u5927\u5bb6\u8ffd\u8e2a\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u504f\u5411\u9501\u5165\u53e3\uff1a<code>http:\/\/hg.openjdk.java.net\/jdk8u\/jdk8u\/hotspot\/file\/9ce27f0a4683\/src\/share\/vm\/interpreter\/bytecodeInterpreter.cpp#l1816<\/code><\/li><li>\u504f\u5411\u64a4\u9500\u5165\u53e3\uff1a<code>http:\/\/hg.openjdk.java.net\/jdk8u\/jdk8u\/hotspot\/file\/9ce27f0a4683\/src\/share\/vm\/interpreter\/interpreterRuntime.cpp#l608<\/code><\/li><li>\u504f\u5411\u9501\u91ca\u653e\u5165\u53e3\uff1a<code>http:\/\/hg.openjdk.java.net\/jdk8u\/jdk8u\/hotspot\/file\/9ce27f0a4683\/src\/share\/vm\/interpreter\/bytecodeInterpreter.cpp#l1923<\/code><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4e2d\u6709\u7591\u95ee\u7684\u5730\u65b9\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\uff0c\u6709\u9519\u8bef\u7684\u5730\u65b9\u8fd8\u8bf7\u5927\u5bb6\u5e2e\u5fd9\u6307\u6b63\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7075\u9b42\u8ffd\u95ee<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>\u8f7b\u91cf\u7ea7\u548c\u91cd\u91cf\u7ea7\u9501\uff0chashcode \u5b58\u5728\u4e86\u4ec0\u4e48\u4f4d\u7f6e\uff1f<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/www.oracle.com\/technetwork\/java\/javase\/tech\/biasedlocking-oopsla2006-preso-150106.pdf<\/li><li>https:\/\/www.oracle.com\/technetwork\/java\/biasedlocking-oopsla2006-wp-149958.pdf<\/li><li>https:\/\/wiki.openjdk.java.net\/display\/HotSpot\/Synchronization#Synchronization-Russel06<\/li><\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5357\">\u4e3a\u4ec0\u4e48\u4eceJava15\u5f00\u59cb\u8981\u7981\u7528\u504f\u5411\u9501\u4e86\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u5728 JDK1.5 \u4e4b\u524d\uff0c\u9762\u5bf9 Java \u5e76\u53d1\u95ee\u9898\uff0c synchronized \u662f\u4e00\u62db\u9c9c\u7684\u89e3\u51b3\u65b9\u6848\uff1a \u666e\u901a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[622,426,1683,202,1682],"class_list":["post-5357","post","type-post","status-publish","format-standard","hentry","category-java","tag-stampedlock","tag-synchronized","tag-next-key-locks","tag-locked","tag-gap-locks"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5357"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5357\/revisions"}],"predecessor-version":[{"id":5358,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5357\/revisions\/5358"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}