{"id":78853,"date":"2026-02-28T12:57:07","date_gmt":"2026-02-28T04:57:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78853.html"},"modified":"2026-02-28T12:57:07","modified_gmt":"2026-02-28T04:57:07","slug":"%e3%80%8cjuc%e3%80%8d%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e4%b8%8e%e7%ae%a1%e7%a8%8b%e6%9c%ba%e5%88%b6%ef%bc%9asynchronized-%e5%8e%9f%e7%90%86%e4%b8%8e%e7%ad%89%e5%be%85%e5%94%a4%e9%86%92%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78853.html","title":{"rendered":"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236"},"content":{"rendered":"<p>\u5f15\u8a00&#xff1a;\u201c\u5728\u5355\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u4ee3\u7801\u7684\u6267\u884c\u5c31\u50cf\u4e00\u6761\u7b14\u76f4\u7684\u9ad8\u901f\u516c\u8def&#xff0c;\u53ef\u9884\u6d4b\u4e14\u5b89\u5168\u3002\u4f46\u5f53\u6211\u4eec\u8e0f\u5165\u591a\u7ebf\u7a0b\u7684\u9886\u5730&#xff0c;\u5171\u4eab\u8d44\u6e90\u5c31\u6210\u4e86\u4e00\u4e2a\u5371\u673a\u56db\u4f0f\u7684\u5341\u5b57\u8def\u53e3\u3002\u5982\u679c\u6ca1\u6709\u7ea2\u7eff\u706f&#xff08;\u540c\u6b65\u673a\u5236&#xff09;&#xff0c;\u8f66\u8f86&#xff08;\u7ebf\u7a0b&#xff09;\u7684\u6a2a\u51b2\u76f4\u649e\u5fc5\u7136\u5bfc\u81f4\u8f66\u7978&#xff08;\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff09;\u3002<\/p>\n<p>\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u6700\u57fa\u672c\u7684\u7ebf\u7a0b\u5b89\u5168\u73b0\u8c61\u5165\u624b&#xff0c;\u4e00\u8def\u5411\u4e0b\u786c\u6838\u5256\u6790 JVM \u662f\u5982\u4f55\u901a\u8fc7 Synchronized \u548c Monitor \u7ba1\u7a0b\u673a\u5236\u6765\u7ef4\u6301\u79e9\u5e8f\u7684&#xff1b;\u540c\u65f6&#xff0c;\u6211\u4eec\u4e5f\u4f1a\u63a2\u8ba8\u7ebf\u7a0b\u4e4b\u95f4\u5982\u4f55\u4f18\u96c5\u5730\u901a\u4fe1&#xff08;Wait\/Notify \u4e0e Park\/Unpark&#xff09;&#xff0c;\u5e76\u7ed3\u5408\u5b9e\u9645\u7684\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\u4e0e\u6b7b\u9501\u6392\u67e5&#xff0c;\u5e2e\u4f60\u771f\u6b63\u9a6f\u670d Java \u5e76\u53d1\u8fd9\u5339\u91ce\u9a6c\u3002\u201d<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u5e76\u53d1\u5b89\u5168\u7684\u5371\u673a\u4e0e Synchronized \u521d\u63a2<\/h4>\n<p>\u5728\u5355\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u4ee3\u7801\u7684\u6267\u884c\u5c31\u50cf\u4e00\u6761\u7b14\u76f4\u7684\u9ad8\u901f\u516c\u8def&#xff0c;\u53ef\u9884\u6d4b\u4e14\u5b89\u5168\u3002\u4f46\u5f53\u6211\u4eec\u8e0f\u5165\u591a\u7ebf\u7a0b\u7684\u9886\u5730&#xff0c;\u5982\u679c\u6ca1\u6709\u201c\u7ea2\u7eff\u706f\u201d\u548c\u4ea4\u8b66&#xff0c;\u8f66\u8f86\u7684\u6a2a\u51b2\u76f4\u649e\u5fc5\u7136\u5bfc\u81f4\u4e25\u91cd\u7684\u8f66\u7978\u3002<\/p>\n<h5>&#x1f7e2; \u4e34\u754c\u533a\u4e0e\u7ade\u6001\u6761\u4ef6&#xff1a;\u8f66\u7978\u662f\u600e\u4e48\u53d1\u751f\u7684&#xff1f;<\/h5>\n<p>\u7b80\u5355\u6765\u8bf4&#xff0c;\u4e34\u754c\u533a\u5c31\u662f\u4e00\u6bb5\u201c\u540c\u65f6\u53ea\u80fd\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u201d\u7684\u4ee3\u7801&#xff0c;\u800c\u8fd9\u90e8\u5206\u4ee3\u7801\u901a\u5e38\u5728\u64cd\u4f5c\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b\u5546\u573a\u91cc\u7684\u5355\u4eba\u6d17\u624b\u95f4&#xff08;\u4e34\u754c\u8d44\u6e90&#xff09;&#xff0c;\u5982\u679c\u95e8\u4e0a\u6ca1\u6709\u9501&#xff0c;\u4e24\u4e2a\u987e\u5ba2&#xff08;\u7ebf\u7a0b&#xff09;\u540c\u65f6\u51b2\u8fdb\u53bb&#xff0c;\u5fc5\u7136\u4f1a\u53d1\u751f\u975e\u5e38\u5c34\u5c2c\u7684\u201c\u51b2\u7a81\u201d\u3002\u5728\u7a0b\u5e8f\u4e2d&#xff0c;\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165\u4e34\u754c\u533a&#xff0c;\u7531\u4e8e CPU \u7ebf\u7a0b\u8c03\u5ea6\u662f\u968f\u673a\u7684&#xff0c;\u4ee3\u7801\u6267\u884c\u7684\u5148\u540e\u987a\u5e8f\u65e0\u6cd5\u9884\u6d4b&#xff0c;\u6700\u7ec8\u5bfc\u81f4\u6570\u636e\u9519\u4e71&#xff0c;\u8fd9\u5c31\u662f\u7ade\u6001\u6761\u4ef6\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3&#xff1a; \u591a\u4e2a\u7ebf\u7a0b\u53ea\u8bfb\u5171\u4eab\u53d8\u91cf\u6ca1\u95ee\u9898&#xff0c;\u4f46\u4e00\u65e6\u591a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u5199\u5171\u4eab\u53d8\u91cf\u4e14\u6307\u4ee4\u53d1\u751f\u4ea4\u9519&#xff0c; BUG \u5c31\u8bde\u751f\u4e86\u3002<\/p>\n<h5>&#x1f7e1; Synchronized \u521d\u63a2&#xff1a;\u6700\u6734\u7d20\u7684\u201c\u7ea2\u7eff\u706f\u201d<\/h5>\n<p>\u4e3a\u4e86\u907f\u514d\u7ade\u6001\u6761\u4ef6&#xff0c;Java \u63d0\u4f9b\u4e86\u6700\u7b80\u5355\u7c97\u66b4\u7684\u963b\u585e\u5f0f\u89e3\u51b3\u65b9\u6848&#xff1a;synchronized \u5173\u952e\u5b57\u3002\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u201c\u5bf9\u8c61\u9501\u201d\u2014\u2014\u7ebf\u7a0b\u8fdb\u5165\u4e34\u754c\u533a\u524d\u5fc5\u987b\u5148\u62ff\u5230\u94a5\u5319&#xff0c;\u6ca1\u62ff\u5230\u94a5\u5319\u7684\u7ebf\u7a0b\u53ea\u80fd\u5728\u95e8\u5916\u5e72\u7b49&#xff08;\u963b\u585e&#xff09;\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u7684\u540e\u7aef\u4e1a\u52a1\u5f00\u53d1\u4e2d&#xff08;\u4f8b\u5982\u5728\u505a\u82cd\u7a79\u5916\u5356\u9879\u76ee\u65f6&#xff0c;\u5904\u7406\u591a\u4e2a\u9a91\u624b\u540c\u65f6\u62a2\u540c\u4e00\u5355\u7684\u5e76\u53d1\u573a\u666f&#xff09;&#xff0c;\u6211\u4eec\u6700\u5e38\u4f7f\u7528\u4ee5\u4e0b\u4e09\u79cd\u52a0\u9501\u59ff\u52bf\u3002\u8fd9\u91cc\u7684\u6838\u5fc3\u8003\u70b9\u53ea\u6709\u4e00\u4e2a&#xff1a;\u4f60\u9501\u7684\u5230\u5e95\u662f\u8c01&#xff1f;<\/p>\n<p>Java<\/p>\n<p>public class OrderService {<br \/>\n    \/\/ 1. \u540c\u6b65\u4ee3\u7801\u5757&#xff08;\u9501\u81ea\u5b9a\u4e49\u5bf9\u8c61&#xff09;&#xff1a;\u7c92\u5ea6\u6700\u7ec6&#xff0c;\u5b9e\u6218\u5efa\u8bae\u9996\u9009<br \/>\n    private final Object lock &#061; new Object();<br \/>\n    public void takeOrder1() {<br \/>\n        synchronized(lock) {<br \/>\n            \/\/ \u53ea\u6709\u62ff\u5230 lock \u5bf9\u8c61\u9501\u7684\u7ebf\u7a0b&#xff0c;\u624d\u80fd\u8fdb\u6765\u63a5\u5355<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u5b9e\u4f8b\u65b9\u6cd5&#xff08;\u9ed8\u8ba4\u9501 this&#xff09;&#xff1a;\u9501\u4f4f\u7684\u662f\u5f53\u524d\u6b63\u5728\u8c03\u7528\u8be5\u65b9\u6cd5\u7684 OrderService \u5b9e\u4f8b<br \/>\n    public synchronized void takeOrder2() {<br \/>\n        \/\/ \u4e34\u754c\u533a\u4ee3\u7801&#8230;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u9759\u6001\u65b9\u6cd5&#xff08;\u9ed8\u8ba4\u9501 Class&#xff09;&#xff1a;\u9501\u4f4f\u7684\u662f\u6574\u4e2a OrderService \u7c7b&#xff0c;\u5c5e\u4e8e\u964d\u7ef4\u6253\u51fb<br \/>\n    public static synchronized void takeOrder3() {<br \/>\n        \/\/ \u4e34\u754c\u533a\u4ee3\u7801&#8230;<br \/>\n    }<br \/>\n}<\/p>\n<h5>&#x1f7e1; \u7ecf\u5178\u68c0\u9a8c&#xff1a;\u7834\u5c40\u6240\u8c13\u7684\u201c\u7ebf\u7a0b\u516b\u9501\u201d<\/h5>\n<p>\u5728\u9762\u8bd5\u5927\u5382\u65f6&#xff0c;\u9762\u8bd5\u5b98\u7279\u522b\u559c\u6b22\u7529\u51fa\u4e00\u5806\u770b\u4f3c\u82b1\u91cc\u80e1\u54e8\u7684\u4ee3\u7801\u8ba9\u4f60\u5224\u65ad\u662f\u5426\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u8fd9\u4e5f\u5c31\u662f\u7ecf\u5178\u7684\u201c\u7ebf\u7a0b\u516b\u9501\u201d\u7b14\u8bd5\u9898\u3002<\/p>\n<p>\u5176\u5b9e\u6839\u672c\u4e0d\u9700\u8981\u6b7b\u8bb0\u786c\u80cc\u90a3\u516b\u79cd\u60c5\u51b5&#xff0c;\u7834\u5c40\u7684\u6838\u5fc3\u5fc3\u6cd5\u53ea\u6709\u4e00\u53e5&#xff1a;\u591a\u7ebf\u7a0b\u7ade\u4e89\u7684&#xff0c;\u5230\u5e95\u662f\u4e0d\u662f\u540c\u4e00\u628a\u9501&#xff1f;<\/p>\n<p>\u6211\u4eec\u76f4\u63a5\u770b\u53cd\u5dee\u6700\u5927\u7684\u4e24\u4e2a\u6781\u7aef\u573a\u666f&#xff1a;<\/p>\n<p>\u573a\u666f A&#xff1a;\u8de8\u9891\u9053\u804a\u5929&#xff08;\u7edd\u5bf9\u4e0d\u5b89\u5168&#xff09;<\/p>\n<p>Java<\/p>\n<p>OrderService s1 &#061; new OrderService();<br \/>\nOrderService s2 &#061; new OrderService();<br \/>\n\/\/ \u9a91\u624b A \u8c03\u7528 s1.takeOrder2() -&gt; \u62ff\u7684\u662f s1 \u7684\u9501<br \/>\n\/\/ \u9a91\u624b B \u8c03\u7528 s2.takeOrder2() -&gt; \u62ff\u7684\u662f s2 \u7684\u9501<\/p>\n<p>\u7ed3\u679c&#xff1a; \u5e76\u53d1\u4e71\u5957\u3002\u56e0\u4e3a\u5b83\u4eec\u62ff\u7684\u662f\u4e24\u628a\u5b8c\u5168\u4e0d\u540c\u7684\u9501&#xff08;\u4e0d\u540c\u7684 this \u5b9e\u4f8b&#xff09;&#xff0c;\u4e92\u76f8\u6839\u672c\u7ba1\u4e0d\u7740\u5bf9\u65b9&#xff0c;\u9632\u7ebf\u5f62\u540c\u865a\u8bbe\u3002<\/p>\n<p>\u573a\u666f B&#xff1a;\u964d\u7ef4\u6253\u51fb&#xff08;\u7edd\u5bf9\u5b89\u5168&#xff09;<\/p>\n<p>Java<\/p>\n<p>OrderService s1 &#061; new OrderService();<br \/>\nOrderService s2 &#061; new OrderService();<br \/>\n\/\/ \u9a91\u624b A \u8c03\u7528 s1.takeOrder3() -&gt; \u62ff\u7684\u662f OrderService.class \u9501<br \/>\n\/\/ \u9a91\u624b B \u8c03\u7528 s2.takeOrder3() -&gt; \u62ff\u7684\u662f OrderService.class \u9501<\/p>\n<p>\u7ed3\u679c&#xff1a; \u5b8c\u7f8e\u540c\u6b65\u3002\u56e0\u4e3a\u65b9\u6cd5\u52a0\u4e86 static&#xff0c;\u9501\u5bf9\u8c61\u53d8\u6210\u4e86\u7c7b\u7684 Class \u5b57\u8282\u7801\u5bf9\u8c61\u3002\u65e0\u8bba\u4f60 new \u51fa\u591a\u5c11\u4e2a OrderService \u5b9e\u4f8b&#xff0c;\u5728 JVM \u4e2d Class \u5bf9\u8c61\u662f\u5168\u5c40\u552f\u4e00\u7684\u3002\u6240\u6709\u7ebf\u7a0b\u62a2\u7684\u90fd\u662f\u540c\u4e00\u628a\u201c\u6700\u9ad8\u7ea7\u522b\u7684\u7c7b\u9501\u201d&#xff0c;\u81ea\u7136\u89c4\u89c4\u77e9\u77e9\u3002<\/p>\n<h4>\u4e8c\u3001\u6df1\u5165 JVM \u8179\u5730\u2014\u2014Monitor \u7ba1\u7a0b\u4e0e Synchronized \u5e95\u5c42\u8fdb\u5316\u53f2<\/h4>\n<p>\u5728\u4e0a\u4e00\u6b21\u7684\u5206\u4eab\u4e2d&#xff0c;\u6211\u4eec\u628a synchronized \u6bd4\u4f5c\u4e86\u4fdd\u62a4\u4e34\u754c\u533a\u7684\u201c\u7ea2\u7eff\u706f\u201d\u3002\u4f46\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7&#xff0c;\u8fd9\u4e2a\u201c\u7ea2\u7eff\u706f\u201d\u5728 Java \u865a\u62df\u673a\u91cc\u5230\u5e95\u957f\u4ec0\u4e48\u6837&#xff1f;\u5b83\u662f\u51ed\u4ec0\u4e48\u62e6\u4f4f\u5176\u4ed6\u7ebf\u7a0b\u7684&#xff1f;<\/p>\n<p>\u4eca\u5929&#xff0c;\u6211\u4eec\u5c31\u6f5c\u5165 JVM \u7684\u8179\u5730&#xff0c;\u62c6\u89e3\u8fd9\u628a\u9501\u7684\u5e95\u5c42\u903b\u8f91\u3002<\/p>\n<h5>&#x1f534; [\u7ea2\u706f\u533a] \u5bf9\u8c61\u5934\u4e0e Monitor&#xff1a;\u9501\u7684\u7269\u7406\u5f62\u6001<\/h5>\n<p>\u5927\u5382\u9762\u8bd5\u559c\u6b22\u62a0\u7ec6\u8282&#xff1a;\u4f60\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u52a0\u9501&#xff0c;\u8fd9\u4e2a\u9501\u7684\u4fe1\u606f\u5230\u5e95\u5b58\u5728\u54ea\u91cc&#xff1f; \u7b54\u6848\u5c31\u5728**\u5bf9\u8c61\u5934&#xff08;Object Header&#xff09;**\u91cc\u3002<\/p>\n<p>\u5728 64 \u4f4d JVM \u4e2d&#xff0c;\u6bcf\u4e2a Java \u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5bf9\u8c61\u5934&#xff0c;\u5176\u4e2d\u6700\u6838\u5fc3\u7684\u90e8\u5206\u53eb Mark Word\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u4e3a\u5bf9\u8c61\u7684\u201c\u8eab\u4efd\u8bc1\u201d&#xff0c;\u91cc\u9762\u8bb0\u5f55\u4e86\u54c8\u5e0c\u7801\u3001GC \u5206\u4ee3\u5e74\u9f84&#xff0c;\u4ee5\u53ca\u6700\u91cd\u8981\u7684\u2014\u2014\u9501\u6807\u5fd7\u4f4d\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528 synchronized \u52a0\u4e0a\u91cd\u91cf\u7ea7\u9501\u65f6&#xff0c;Mark Word \u7684\u72b6\u6001\u4f1a\u88ab\u4fee\u6539&#xff0c;\u5b83\u7684\u6307\u9488\u4f1a\u6307\u5411\u4e00\u4e2a\u53eb\u505a Monitor&#xff08;\u7ba1\u7a0b\/\u76d1\u89c6\u5668&#xff09; \u7684\u5bf9\u8c61\u3002Monitor \u5c31\u662f\u771f\u6b63\u7ef4\u6301\u5e76\u53d1\u79e9\u5e8f\u7684\u6838\u5fc3\u5927\u8111\u3002<\/p>\n<p>Monitor \u5185\u90e8\u6709\u4e09\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u7ec4\u4ef6&#xff1a;<\/p>\n<li>Owner&#xff08;\u4e3b\u4eba&#xff09;&#xff1a;\u5f53\u524d\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u3002\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u3002<\/li>\n<li>EntryList&#xff08;\u7b49\u5f85\u8d70\u5eca&#xff09;&#xff1a;\u53cc\u5411\u94fe\u8868\u3002\u90a3\u4e9b\u6ca1\u62a2\u5230\u9501\u7684\u7ebf\u7a0b&#xff0c;\u5168\u90fd\u88ab\u6254\u5230\u8fd9\u91cc\u963b\u585e\u6392\u961f\u3002<\/li>\n<li>WaitSet&#xff08;\u4f11\u606f\u5ba4&#xff09;&#xff1a;\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u4e3b\u52a8\u8c03\u7528 wait() \u653e\u5f03\u9501\u7684\u7ebf\u7a0b\u5728\u8fd9\u91cc\u4f11\u606f&#xff08;\u4e0b\u4e00\u7ae0\u7ec6\u8bb2&#xff09;\u3002<\/li>\n<p>&#x1f4a1;\u9762\u8bd5\u5b98&#xff1a; \u5047\u8bbe\u7ebf\u7a0b A \u62ff\u5230\u4e86\u9501&#xff0c;\u6b63\u5728\u6267\u884c\u3002\u6b64\u65f6\u7ebf\u7a0b B \u548c C \u6765\u4e86&#xff0c;\u53d1\u73b0 Monitor \u7684 Owner \u4e0d\u662f\u81ea\u5df1&#xff0c;\u5b83\u4eec\u4f1a\u7ecf\u5386\u4ec0\u4e48&#xff1f; \u56de\u7b54&#xff1a; \u5b83\u4eec\u4f1a\u8fdb\u5165 Monitor \u7684 EntryList&#xff0c;\u5e76\u4e14\u7ebf\u7a0b\u72b6\u6001\u4ece RUNNABLE \u8f6c\u53d8\u4e3a BLOCKED&#xff08;\u963b\u585e&#xff09;&#xff0c;\u6b64\u65f6\u5b83\u4eec\u4f1a\u4ea4\u51fa CPU \u6267\u884c\u6743&#xff0c;\u5904\u4e8e\u6302\u8d77\u72b6\u6001\u3002\u5f53\u7ebf\u7a0b A \u6267\u884c\u5b8c\u540c\u6b65\u4ee3\u7801\u5757&#xff0c;\u4f1a\u5c06 Owner \u7f6e\u4e3a\u7a7a&#xff0c;\u5e76\u5524\u9192 EntryList \u4e2d\u7684\u7ebf\u7a0b\u3002\u6ce8\u610f&#xff0c;Java \u4e2d\u7684 synchronized \u5524\u9192\u662f\u975e\u516c\u5e73\u7684&#xff0c;B \u548c C \u8c01\u62a2\u5230\u7b97\u8c01\u7684&#xff0c;\u751a\u81f3\u5982\u679c\u6b64\u65f6\u521a\u5de7\u6765\u4e86\u4e00\u4e2a\u7ebf\u7a0b D&#xff0c;D \u4e5f\u6709\u53ef\u80fd\u76f4\u63a5\u63d2\u961f\u62a2\u8d70\u3002<\/p>\n<h5>&#x1f7e1; [\u9ec4\u706f\u533a] \u5b57\u8282\u7801\u89c6\u89d2&#xff1a;\u53cc\u4fdd\u9669\u7684\u9000\u51fa\u673a\u5236<\/h5>\n<p>\u65e2\u7136\u9501\u662f\u9760 Monitor \u63a7\u5236\u7684&#xff0c;\u90a3\u4ee3\u7801\u662f\u600e\u4e48\u548c Monitor \u5173\u8054\u8d77\u6765\u7684&#xff1f;\u6211\u4eec\u6765\u770b\u4e00\u6bb5\u6781\u7b80\u7684\u540c\u6b65\u5757\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801&#xff1a;<\/p>\n<p>Java<\/p>\n<p>Object lock &#061; new Object();<br \/>\nsynchronized (lock) {<br \/>\n    System.out.println(&#034;ok&#034;);<br \/>\n}<\/p>\n<p>\u5728\u5e95\u5c42\u5b57\u8282\u7801\u4e2d&#xff0c;\u4f60\u4f1a\u770b\u5230\u4e24\u4e2a\u6838\u5fc3\u6307\u4ee4&#xff1a;<\/p>\n<ul>\n<li>monitorenter&#xff1a;\u5c1d\u8bd5\u5c06 lock \u5bf9\u8c61\u7684 Mark Word \u6307\u5411 Monitor&#xff0c;\u5e76\u5c06 Owner \u8bbe\u4e3a\u81ea\u5df1\u3002<\/li>\n<li>monitorexit&#xff1a;\u91cd\u7f6e Mark Word&#xff0c;\u5524\u9192 EntryList\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u6709\u4e24\u4e2a **monitorexit**&#xff1f; \u5f88\u7b80\u5355&#xff0c;\u9632\u6b7b\u9501\u3002\u7b2c\u4e00\u4e2a monitorexit \u662f\u6b63\u5e38\u8d70\u5b8c\u540c\u6b65\u5757\u91ca\u653e\u9501&#xff1b;\u7b2c\u4e8c\u4e2a\u662f\u7f16\u8bd1\u5668\u901a\u8fc7\u5f02\u5e38\u8868&#xff08;Exception table&#xff09;\u9690\u5f0f\u52a0\u5165\u7684\u3002\u8fd9\u5c31\u4fdd\u8bc1\u4e86\u54ea\u6015\u4f60\u7684\u4ee3\u7801\u5728\u540c\u6b65\u5757\u91cc\u629b\u4e86\u5f02\u5e38\u5d29\u6e83\u4e86&#xff0c;JVM \u4e5f\u80fd\u515c\u5e95\u5e2e\u4f60\u628a\u9501\u91ca\u653e\u6389&#xff0c;\u4e0d\u81f3\u4e8e\u8ba9\u5176\u4ed6\u6392\u961f\u7684\u7ebf\u7a0b\u7b49\u5230\u5929\u8352\u5730\u8001\u3002<\/p>\n<h5>&#x1f534; [\u7ea2\u706f\u533a] \u9501\u5347\u7ea7\u5168\u8fc7\u7a0b&#xff1a;JVM \u7684\u514b\u5236\u4e0e\u8fdb\u5316<\/h5>\n<p>\u65e9\u671f\u7684 Java \u4e2d&#xff0c;synchronized \u662f\u65e0\u8111\u7684\u91cd\u91cf\u7ea7\u9501&#xff08;\u76f4\u63a5\u4e0a Monitor&#xff09;\u3002\u4f46\u7533\u8bf7\u91cd\u91cf\u7ea7\u9501\u9700\u8981\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684 Mutex Lock&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u7528\u6237\u6001\u5230\u5185\u6838\u6001\u7684\u5207\u6362&#xff0c;\u6781\u4e3a\u6d88\u8017\u6027\u80fd\u3002<\/p>\n<p>JVM \u56e2\u961f\u540e\u6765\u53d1\u73b0\u4e86\u4e00\u4e2a\u89c4\u5f8b&#xff1a;\u5728\u771f\u5b9e\u4e1a\u52a1\u4e2d&#xff0c;\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6839\u672c\u5c31\u6ca1\u6709\u591a\u7ebf\u7a0b\u7ade\u4e89&#xff0c;\u6216\u8005\u7ade\u4e89\u7684\u65f6\u95f4\u6781\u77ed\u3002 \u4e3a\u4e86\u8fd9\u70b9\u6982\u7387\u53bb\u9891\u7e41\u5207\u6362\u5185\u6838\u6001&#xff0c;\u592a\u4e8f\u4e86\u3002\u4e8e\u662f&#xff0c;JDK 6 \u5f15\u5165\u4e86\u6781\u5176\u4f18\u96c5\u7684\u9501\u5347\u7ea7\u673a\u5236&#xff1a;\u53ea\u80fd\u5347\u7ea7&#xff0c;\u4e0d\u80fd\u964d\u7ea7\u3002<\/p>\n<p>1. \u65e0\u9501 -&gt; \u504f\u5411\u9501&#xff08;Biased Lock&#xff09; \u201c\u504f\u5411\u7b2c\u4e00\u4e2a\u6765\u7684\u4eba\u201d\u3002\u5982\u679c\u8fd9\u6bb5\u65f6\u95f4\u53ea\u6709\u7ebf\u7a0b A \u5728\u53cd\u590d\u8fdb\u51fa\u540c\u6b65\u5757&#xff0c;JVM \u4f1a\u76f4\u63a5\u5728\u5bf9\u8c61\u7684 Mark Word \u91cc\u6253\u4e0a\u7ebf\u7a0b A \u7684 ID&#xff08;\u8fd9\u5c31\u662f CAS \u64cd\u4f5c&#xff09;\u3002\u4e0b\u6b21 A \u518d\u6765&#xff0c;\u770b\u4e00\u773c ID \u662f\u81ea\u5df1&#xff0c;\u76f4\u63a5\u653e\u884c&#xff0c;\u51e0\u4e4e\u96f6\u5f00\u9500\u3002<\/p>\n<p>2. \u504f\u5411\u9501 -&gt; \u8f7b\u91cf\u7ea7\u9501&#xff08;Lightweight Lock&#xff09; \u5982\u679c\u6709\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b B \u6765\u4e86&#xff0c;\u53d1\u73b0\u9501\u5df2\u7ecf\u504f\u5411\u4e86 A&#xff0c;\u504f\u5411\u72b6\u6001\u5c31\u4f1a\u64a4\u9500\u3002\u6b64\u65f6\u5982\u679c A \u548c B \u7684\u6267\u884c\u65f6\u95f4\u662f\u9519\u5f00\u7684&#xff08;\u6ca1\u6709\u53d1\u751f\u771f\u6b63\u7684\u540c\u65f6\u62a2\u593a&#xff09;&#xff0c;JVM \u5c31\u4f1a\u5347\u7ea7\u4e3a\u8f7b\u91cf\u7ea7\u9501\u3002 \u7ebf\u7a0b\u4f1a\u5728\u81ea\u5df1\u7684\u6808\u5e27\u91cc\u521b\u5efa\u4e00\u4e2a\u201c\u9501\u8bb0\u5f55&#xff08;Lock Record&#xff09;\u201d&#xff0c;\u5c1d\u8bd5\u7528 CAS \u64cd\u4f5c\u628a\u5bf9\u8c61\u7684 Mark Word \u66ff\u6362\u4e3a\u6307\u5411\u81ea\u5df1\u9501\u8bb0\u5f55\u7684\u6307\u9488\u3002\u6210\u529f\u4e86&#xff0c;\u5c31\u62ff\u5230\u4e86\u8f7b\u91cf\u7ea7\u9501\u3002<\/p>\n<p>3. \u8f7b\u91cf\u7ea7\u9501 -&gt; \u91cd\u91cf\u7ea7\u9501&#xff08;Heavyweight Lock&#xff0c;\u5373\u9501\u81a8\u80c0&#xff09; \u5982\u679c\u5728\u5347\u7ea7\u8f7b\u91cf\u7ea7\u9501\u7684\u8fc7\u7a0b\u4e2d&#xff0c;B \u53d1\u73b0 CAS \u5931\u8d25\u4e86\u2014\u2014\u8bf4\u660e\u6b64\u65f6 A \u6b63\u5728\u91cc\u9762\u6ca1\u51fa\u6765&#xff0c;\u53d1\u751f\u4e86\u771f\u6b63\u7684\u7ade\u4e89&#xff01;\u8f7b\u91cf\u7ea7\u9501\u515c\u4e0d\u4f4f\u4e86&#xff0c;\u5fc5\u987b\u8fdb\u884c\u201c\u9501\u81a8\u80c0\u201d\u3002\u6b64\u65f6&#xff0c;JVM \u624d\u4f1a\u8001\u8001\u5b9e\u5b9e\u53bb\u5806\u91cc\u7533\u8bf7\u521a\u624d\u8bb2\u7684\u90a3\u4e2a Monitor \u5bf9\u8c61&#xff0c;\u5c06\u72b6\u6001\u5f7b\u5e95\u8f6c\u4e3a\u91cd\u91cf\u7ea7\u9501&#xff0c;B \u4e56\u4e56\u8fdb\u5165 EntryList \u963b\u585e\u3002<\/p>\n<p>&#x1f6e0;\ufe0f \u771f\u5b9e\u4e1a\u52a1\u9632\u5751\u6307\u5357&#xff1a; \u4ee5\u5f00\u53d1\u82cd\u7a79\u5916\u5356\u7684\u5e76\u53d1\u62a2\u5355\u6a21\u5757\u4e3a\u4f8b\u3002\u5982\u679c\u5728\u51cc\u6668\u5355\u91cf\u6781\u5c11\u65f6&#xff0c;\u53ef\u80fd\u5076\u5c14\u53ea\u6709\u4e00\u4e2a\u9a91\u624b\u8bbf\u95ee\u62a2\u5355\u63a5\u53e3&#xff0c;\u6b64\u65f6 JVM \u7528\u7684\u662f\u504f\u5411\u9501&#xff0c;\u6027\u80fd\u6781\u9ad8\u3002 \u4f46\u5982\u679c\u5728\u4e2d\u5348\u7528\u9910\u9ad8\u5cf0\u671f&#xff0c;\u4e0a\u767e\u4e2a\u9a91\u624b\u75af\u72c2\u70b9\u51fb\u62a2\u5355&#xff0c;\u5e76\u53d1\u91cf\u66b4\u589e\u3002\u5982\u679c\u4f60\u8fd8\u5728\u6838\u5fc3\u903b\u8f91\u5916\u56f4\u52a0 synchronized&#xff0c;\u9501\u4f1a\u8fc5\u901f\u81a8\u80c0\u4e3a\u91cd\u91cf\u7ea7\u9501&#xff0c;\u5bfc\u81f4\u5927\u91cf\u5904\u7406\u7ebf\u7a0b BLOCKED&#xff0c;\u4e0a\u4e0b\u6587\u5207\u6362\u5267\u589e&#xff0c;CPU \u98d9\u5347\u4f46\u7cfb\u7edf\u541e\u5410\u91cf\u65ad\u5d16\u5f0f\u4e0b\u8dcc\u3002 \u9632\u5751\u7b56\u7565&#xff1a; \u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b&#xff0c;\u76f4\u63a5\u7981\u7528\u504f\u5411\u9501&#xff08;-XX:-UseBiasedLocking&#xff09;\u53ef\u4ee5\u51cf\u5c11\u64a4\u9500\u504f\u5411\u9501\u5e26\u6765\u7684\u505c\u987f&#xff08;STW&#xff09;&#xff1b;\u540c\u65f6&#xff0c;\u5c3d\u91cf\u7f29\u5c0f synchronized \u7684\u5305\u88f9\u8303\u56f4&#xff08;\u51cf\u5c0f\u4e34\u754c\u533a&#xff09;&#xff0c;\u6216\u8005\u5e72\u8106\u4f7f\u7528\u57fa\u4e8e\u4e50\u89c2\u9501\u7684 CAS&#xff08;\u5982\u539f\u5b50\u7c7b&#xff09;\u6216 Redis \u5206\u5e03\u5f0f\u9501\u6765\u91cd\u6784\u62a2\u5355\u903b\u8f91\u3002<\/p>\n<h5>&#x1f7e2; [\u7eff\u706f\u533a] \u9501\u4f18\u5316&#xff1a;JVM \u7684\u201c\u5fae\u64cd\u201d<\/h5>\n<p>\u4e3a\u4e86\u69a8\u5e72\u6700\u540e\u4e00\u70b9\u6027\u80fd&#xff0c;JVM \u5728\u7f16\u8bd1\u9636\u6bb5\u548c\u8fd0\u884c\u671f\u8fd8\u4f1a\u505a\u4e00\u4e9b\u4f18\u5316&#xff1a;<\/p>\n<ul>\n<li>\u81ea\u65cb\u9501&#xff1a; \u5728\u5347\u7ea7\u4e3a\u91cd\u91cf\u7ea7\u9501\u4e4b\u524d&#xff0c;\u7ebf\u7a0b\u83b7\u53d6\u9501\u5931\u8d25\u65f6\u4e0d\u4f1a\u7acb\u523b\u6302\u8d77\u81ea\u5df1&#xff0c;\u800c\u662f\u539f\u5730\u5faa\u73af&#xff08;\u81ea\u65cb&#xff09;\u51e0\u6b21\u3002\u8fd9\u5c31\u597d\u6bd4\u7b49\u7ea2\u706f\u65f6\u4f60\u4e0d\u7184\u706b&#xff0c;\u968f\u65f6\u51c6\u5907\u4e00\u811a\u6cb9\u95e8\u8e29\u4e0b\u53bb&#xff0c;\u7528\u77ed\u6682\u7684 CPU \u7a7a\u8f6c\u6362\u53d6\u907f\u514d\u7ebf\u7a0b\u5207\u6362\u7684\u5de8\u5927\u5f00\u9500\u3002<\/li>\n<li>\u9501\u6d88\u9664&#xff1a; JVM \u901a\u8fc7\u9003\u9038\u5206\u6790&#xff0c;\u53d1\u73b0\u4f60\u52a0\u9501\u7684\u5bf9\u8c61&#xff08;\u6bd4\u5982\u67d0\u4e2a\u65b9\u6cd5\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf&#xff09;\u6839\u672c\u4e0d\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8bbf\u95ee\u5230&#xff0c;\u5c31\u4f1a\u5728\u7f16\u8bd1\u65f6\u81ea\u52a8\u628a\u8fd9\u628a\u9501\u53bb\u6389\u3002<\/li>\n<li>\u9501\u7c97\u5316&#xff1a; \u5982\u679c\u4f60\u5728\u4e00\u4e2a for \u5faa\u73af\u91cc\u75af\u72c2\u5bf9\u540c\u4e00\u4e2a\u5bf9\u8c61\u52a0\u9501\u89e3\u9501&#xff0c;JVM \u4f1a\u770b\u4e0d\u4e0b\u53bb&#xff0c;\u76f4\u63a5\u628a\u52a0\u9501\u8303\u56f4\u6269\u5927\u5230\u5faa\u73af\u5916\u90e8&#xff0c;\u53ea\u52a0\u4e00\u6b21\u3002<\/li>\n<\/ul>\n<h4>\u4e09\u3001\u6253\u7834\u4fe1\u606f\u5b64\u5c9b\u2014\u2014\u7ebf\u7a0b\u95f4\u534f\u4f5c\u4e0e\u901a\u4fe1\u673a\u5236<\/h4>\n<h5>&#x1f7e1; [\u9ec4\u706f\u533a] Wait\/Notify \u4e0e Sleep&#xff1a;\u5e26\u4e0d\u5e26\u94a5\u5319\u7684\u533a\u522b<\/h5>\n<p>\u4e3a\u4e86\u8ba9\u7ebf\u7a0b\u5728\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\u6b47\u7740&#xff0c;Java \u63d0\u4f9b\u4e86 wait() \/ notify() \u673a\u5236\u3002\u5b83\u4fe9\u5fc5\u987b\u914d\u5408 synchronized \u4e00\u8d77\u4f7f\u7528&#xff08;\u4e5f\u5c31\u662f\u5fc5\u987b\u5148\u62ff\u5230\u5bf9\u8c61\u9501&#xff09;\u3002<\/p>\n<p>\u4e0d\u8981\u53bb\u80cc\u516b\u80a1\u6587\u91cc\u7684\u6982\u5ff5&#xff0c;\u53ea\u9700\u8981\u8bb0\u4f4f\u4e00\u4e2a\u901a\u4fd7\u7684\u6bd4\u55bb&#xff1a;<\/p>\n<ul>\n<li>**sleep()**&#xff1a;\u76f8\u5f53\u4e8e\u4f60\u62ff\u7740\u6d17\u624b\u95f4\u7684\u94a5\u5319\u5728\u9a6c\u6876\u4e0a\u7761\u7740\u4e86\u3002\u4f60\u4e0d\u9192&#xff0c;\u8c01\u4e5f\u8fdb\u4e0d\u6765&#xff08;\u4e0d\u91ca\u653e\u9501&#xff0c;\u4f46\u91ca\u653e CPU&#xff09;\u3002<\/li>\n<li>**wait()**&#xff1a;\u76f8\u5f53\u4e8e\u4f60\u53d1\u73b0\u6d17\u624b\u95f4\u6ca1\u624b\u7eb8\u4e86&#xff0c;\u4e3b\u52a8\u628a\u94a5\u5319\u4ea4\u51fa\u53bb&#xff0c;\u8dd1\u5230\u95e8\u5916\u7684\u4f11\u606f\u5ba4&#xff08;Monitor \u7684 WaitSet&#xff09;\u53bb\u7b49&#xff08;\u91ca\u653e\u9501&#xff0c;\u91ca\u653e CPU&#xff09;\u3002\u76f4\u5230\u522b\u4eba\u9001\u6765\u4e86\u7eb8&#xff0c;\u5e76\u5927\u558a\u4e00\u58f0 notify()&#xff0c;\u4f60\u624d\u91cd\u65b0\u56de\u53bb\u6392\u961f\u62a2\u94a5\u5319\u3002<\/li>\n<\/ul>\n<h5>&#x1f534; [\u7ea2\u706f\u533a] \u865a\u5047\u5524\u9192\u4e0e while \u5faa\u73af\u68c0\u6d4b<\/h5>\n<p>\u5982\u679c\u4f60\u5728\u4ee3\u7801\u91cc\u7528\u4e86 wait()&#xff0c;\u5927\u5382\u9762\u8bd5\u5b98\u4e00\u5b9a\u4f1a\u987a\u85e4\u6478\u74dc&#xff0c;\u629b\u51fa\u8fd9\u4e2a\u9ad8\u9891\u7684\u5b9e\u6218\u7ea2\u706f\u95ee\u9898&#xff1a;\u201c\u865a\u5047\u5524\u9192&#xff08;Spurious Wakeup&#xff09;\u542c\u8fc7\u5417&#xff1f;\u4ee3\u7801\u5e94\u8be5\u600e\u4e48\u5199&#xff1f;\u201d<\/p>\n<p>\u5728\u4e1a\u52a1\u573a\u666f\u4e2d&#xff0c;\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528 notifyAll() \u6765\u5524\u9192\u4f11\u606f\u5ba4\u91cc\u7684\u6240\u6709\u7ebf\u7a0b\u3002\u5047\u8bbe\u5916\u5356\u7cfb\u7edf\u91cc\u6709\u4e24\u4e2a\u7ebf\u7a0b&#xff1a;\u5c0f\u54e5 A \u7b49\u7740\u9001\u201c\u70e7\u70e4\u201d&#xff0c;\u5c0f\u54e5 B \u7b49\u7740\u9001\u201c\u5976\u8336\u201d\u3002\u6b64\u65f6\u53a8\u623f\u4ea7\u51fa\u4e86\u4e00\u4efd\u201c\u5976\u8336\u201d&#xff0c;\u5e76\u558a\u4e86\u4e00\u58f0 notifyAll()\u3002<\/p>\n<p>\u53cd\u9762\u6559\u6750&#xff08;\u707e\u96be\u73b0\u573a&#xff09;&#xff1a;\u4f7f\u7528 **if** \u5224\u65ad<\/p>\n<p>Java<\/p>\n<p>synchronized (lock) {<br \/>\n    if (!hasBBQ) { \/\/ \u5c0f\u54e5 A \u9192\u4e86&#xff0c;\u4f46\u6ca1\u70e7\u70e4&#xff0c;\u76f4\u63a5\u5411\u4e0b\u6267\u884c&#xff0c;\u9001\u4e86\u4e2a\u5bc2\u5bde&#xff01;<br \/>\n        lock.wait();<br \/>\n    }<br \/>\n    \/\/ \u9192\u6765\u540e\u7ee7\u7eed\u6267\u884c\u9001\u5916\u5356\u903b\u8f91&#8230;<br \/>\n}<\/p>\n<p>\u5927\u5382\u9632\u5751\u6307\u5357&#xff1a; \u5c0f\u54e5 A \u88ab\u8bef\u5524\u9192\u4e86&#xff0c;\u5982\u679c\u7528 if&#xff0c;\u4ed6\u9192\u6765\u540e\u4e0d\u4f1a\u518d\u6b21\u68c0\u67e5\u6761\u4ef6&#xff0c;\u76f4\u63a5\u5f80\u4e0b\u8d70&#xff0c;\u5bfc\u81f4\u4e1a\u52a1\u903b\u8f91\u51fa\u73b0\u4e25\u91cd Bug\u3002<\/p>\n<p>\u6807\u51c6\u7b54\u6848&#xff1a;\u4f7f\u7528 **while** \u5faa\u73af<\/p>\n<p>Java<\/p>\n<p>synchronized (lock) {<br \/>\n    while (!hasBBQ) { \/\/ \u9192\u6765\u7b2c\u4e00\u4ef6\u4e8b&#xff1a;\u518d\u770b\u4e00\u773c\u5230\u5e95\u6709\u6ca1\u6709\u6211\u7684\u5916\u5356<br \/>\n        lock.wait();  \/\/ \u4e0d\u662f\u6211\u7684&#xff1f;\u7ee7\u7eed\u4e56\u4e56\u8eba\u4e0b\u7761<br \/>\n    }<br \/>\n    \/\/ \u9192\u6765\u540e\u7ee7\u7eed\u6267\u884c\u9001\u5916\u5356\u903b\u8f91&#8230;<br \/>\n}<\/p>\n<p>&#x1f4a1; \u9762\u8bd5\u5b98&#xff1a; \u4e3a\u4ec0\u4e48\u4e0d\u63a8\u8350\u7528 notify() \u800c\u63a8\u8350 notifyAll()&#xff1f; \u56de\u7b54&#xff1a;notify() \u53ea\u80fd\u968f\u673a\u5524\u9192 WaitSet \u4e2d\u7684\u4e00\u4e2a\u7ebf\u7a0b\u3002\u5982\u679c\u53a8\u623f\u4ea7\u51fa\u4e86\u5976\u8336&#xff0c;\u5374\u7528 notify() \u968f\u673a\u5524\u9192\u4e86\u7b49\u70e7\u70e4\u7684\u5c0f\u54e5 A&#xff0c;\u5c0f\u54e5 A \u9192\u6765\u4e00\u770b\u4e0d\u662f\u81ea\u5df1\u7684&#xff0c;\u7ee7\u7eed wait()\u3002\u800c\u771f\u6b63\u7b49\u5976\u8336\u7684\u5c0f\u54e5 B \u8fd8\u5728\u6b7b\u7761\u3002\u8fd9\u5c31\u5bfc\u81f4\u4e86\u4fe1\u53f7\u4e22\u5931&#xff0c;\u7a0b\u5e8f\u5f7b\u5e95\u5361\u6b7b\u3002\u7528 notifyAll() \u914d\u5408 while \u5faa\u73af&#xff0c;\u624d\u662f\u6700\u5065\u58ee\u7684\u5de5\u4e1a\u7ea7\u5199\u6cd5\u3002<\/p>\n<h5>&#x1f534; [\u7ea2\u706f\u533a] LockSupport&#xff1a;\u5927\u5382\u5e95\u5c42\u7684\u201c\u53d1\u724c\u5b98\u201d<\/h5>\n<p>wait\/notify \u867d\u7136\u7ecf\u5178&#xff0c;\u4f46\u592a\u7b28\u91cd\u4e86\u3002\u5b83\u5fc5\u987b\u7ed1\u5b9a synchronized&#xff0c;\u800c\u4e14\u6781\u5176\u8bb2\u7a76\u5148\u540e\u987a\u5e8f\u2014\u2014\u5982\u679c\u5728 wait() \u4e4b\u524d\u5bf9\u65b9\u5c31\u5148 notify() \u4e86&#xff0c;\u8fd9\u4e2a\u5524\u9192\u4fe1\u53f7\u5c31\u6c38\u8fdc\u4e22\u5931\u4e86&#xff0c;\u7ebf\u7a0b\u4f1a\u6b7b\u7b49\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#xff0c;JDK \u5e76\u53d1\u5305\u7684\u5e95\u5c42&#xff08;\u5305\u62ec\u5927\u540d\u9f0e\u9f0e\u7684 AQS \u6846\u67b6&#xff09;\u90fd\u91c7\u7528\u4e86\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u7ebf\u7a0b\u539f\u8bed&#xff1a;LockSupport.park() \u548c LockSupport.unpark()\u3002\u8fd9\u91cc\u662f\u5f88\u591a\u666e\u901a\u5f00\u53d1\u8005\u7684\u77e5\u8bc6\u76f2\u533a\u3002<\/p>\n<p>\u5b83\u4e0d\u9700\u8981\u52a0\u9501&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u6302\u8d77\u548c\u5524\u9192\u6307\u5b9a\u7684\u7ebf\u7a0b\u3002\u5b83\u7684\u5e95\u5c42\u903b\u8f91\u5c31\u50cf\u662f\u4e00\u4e2a\u53d1\u724c\u673a\u5236&#xff1a;\u7ef4\u62a4\u4e86\u4e00\u4e2a\u53ea\u80fd\u5b58 1 \u6216 0 \u7684 _counter&#xff08;\u8bb8\u53ef&#xff09;\u3002<\/p>\n<p>\u6838\u5fc3\u5de5\u4f5c\u6d41&#xff08;_counter \u673a\u5236&#xff09;&#xff1a;<\/p>\n<li>**unpark(thread)**** \u52a8\u4f5c&#xff1a;** \u7ed9\u6307\u5b9a\u7684\u7ebf\u7a0b\u53d1\u4e00\u5f20\u8bb8\u53ef\u3002_counter \u53d8\u6210 1\u3002&#xff08;\u6ce8\u610f&#xff1a;\u54ea\u6015\u4f60\u8fde\u7eed unpark 10 \u6b21&#xff0c;_counter \u4e5f\u662f 1&#xff0c;\u4e0d\u4f1a\u7d2f\u52a0&#xff09;\u3002<\/li>\n<li>**park()**** \u52a8\u4f5c&#xff1a;** \u68c0\u67e5\u81ea\u5df1\u6709\u6ca1\u6709\u8bb8\u53ef\u3002\u5982\u679c\u6709&#xff08;_counter \u4e3a 1&#xff09;&#xff0c;\u5c31\u628a\u8bb8\u53ef\u6d88\u8017\u6389&#xff08;\u53d8\u4e3a 0&#xff09;&#xff0c;\u7136\u540e\u76f4\u63a5\u901a\u884c&#xff0c;\u4e0d\u963b\u585e&#xff01;\u5982\u679c\u6ca1\u6709\u8bb8\u53ef&#xff08;_counter \u4e3a 0&#xff09;&#xff0c;\u7ebf\u7a0b\u76f4\u63a5\u88ab\u6302\u8d77\u3002<\/li>\n<p>&#x1f6e0;\ufe0f \u5e95\u5c42\u539f\u7406\u5256\u6790&#xff1a; \u6b63\u662f\u56e0\u4e3a\u8fd9\u4e2a _counter \u7684\u5b58\u5728&#xff0c;LockSupport \u5b8c\u7f8e\u89e3\u51b3\u4e86 wait\/notify \u987a\u5e8f\u9519\u4e71\u7684\u95ee\u9898\u3002 \u5047\u8bbe\u751f\u4ea7\u8005\u901f\u5ea6\u6781\u5feb&#xff0c;\u5728\u6d88\u8d39\u8005\u6267\u884c park() \u4e4b\u524d&#xff0c;\u5c31\u5df2\u7ecf\u6267\u884c\u4e86 unpark(\u6d88\u8d39\u8005)\u3002\u6b64\u65f6\u6d88\u8d39\u8005\u7684 _counter \u5df2\u7ecf\u88ab\u7f6e\u4e3a\u4e86 1\u3002\u5f53\u6d88\u8d39\u8005\u968f\u540e\u6267\u884c park() \u65f6&#xff0c;\u53d1\u73b0\u5df2\u7ecf\u6709\u8bb8\u53ef\u4e86&#xff0c;\u5c31\u4f1a\u76f4\u63a5\u7ee7\u7eed\u5f80\u4e0b\u8dd1&#xff0c;\u800c\u4e0d\u4f1a\u50cf wait \u90a3\u6837\u88ab\u6b7b\u6b7b\u5361\u4f4f\u3002<\/p>\n<p>\u6781\u7b80\u4f2a\u4ee3\u7801&#xff1a;<\/p>\n<p>Java<\/p>\n<p>Thread t1 &#061; new Thread(() -&gt; {<br \/>\n    \/\/ \u5373\u4f7f\u4e3b\u7ebf\u7a0b\u5df2\u7ecf\u63d0\u524d unpark \u4e86&#xff0c;\u8fd9\u91cc\u4e5f\u4e0d\u4f1a\u88ab\u963b\u585e<br \/>\n    LockSupport.park();<br \/>\n    System.out.println(&#034;\u6536\u5230\u8bb8\u53ef&#xff0c;\u5f00\u59cb\u6d88\u8d39&#8230;&#034;);<br \/>\n});<br \/>\nt1.start();<\/p>\n<p>\/\/ \u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u76f4\u63a5\u7cbe\u51c6\u5524\u9192 t1&#xff0c;\u4e0d\u9700\u8981 synchronized \u62a2\u9501<br \/>\nLockSupport.unpark(t1);<\/p>\n<h4>\u56db\u3001\u7eb8\u4e0a\u5f97\u6765\u7ec8\u89c9\u6d45\u2014\u2014\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218<\/h4>\n<h5>&#x1f7e1; [\u9ec4\u706f\u533a] \u4fdd\u62a4\u6027\u6682\u505c\u6a21\u5f0f (Guarded Suspension)<\/h5>\n<p>\u4e00\u53e5\u8bdd\u901a\u4fd7\u7406\u89e3&#xff1a; \u5c31\u50cf\u4f60\u70b9\u4e86\u4e00\u4efd\u5916\u5356&#xff0c;\u5fc5\u987b\u5750\u5728\u5bb6\u91cc\u7b49\u9a91\u624b\u9001\u8fbe&#xff0c;\u62ff\u5230\u5916\u5356&#xff08;\u7ed3\u679c&#xff09;\u540e\u624d\u80fd\u63a5\u7740\u5403\u996d\u3002\u4e00\u4e2a\u7ebf\u7a0b\u5fc5\u987b\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4ea7\u751f\u7ed3\u679c\u624d\u80fd\u7ee7\u7eed\u5f80\u4e0b\u8d70&#xff0c;\u8fd9\u5c31\u662f\u4fdd\u62a4\u6027\u6682\u505c\u3002\u6211\u4eec\u5e73\u65f6\u9ad8\u9891\u4f7f\u7528\u7684 Thread.join() \u6216\u8005 Future.get()&#xff0c;\u5e95\u5c42\u5168\u90fd\u662f\u8fd9\u5957\u601d\u60f3\u3002<\/p>\n<p>\u8fd9\u91cc\u6700\u6838\u5fc3\u7684\u5de5\u7a0b\u7d20\u517b\u662f&#xff1a;\u7edd\u5bf9\u4e0d\u80fd\u6b7b\u7b49\u3002 \u5728\u771f\u5b9e\u7684\u4e1a\u52a1\u4e2d&#xff0c;\u5982\u679c\u4f60\u8c03\u7528\u7684\u5916\u90e8\u63a5\u53e3\u8d85\u65f6\u4e86&#xff0c;\u4f60\u7684\u7ebf\u7a0b\u4e0d\u80fd\u4e00\u76f4\u5361\u5728 wait() \u4e0a&#xff0c;\u5fc5\u987b\u6709\u4e00\u4e2a\u8d85\u65f6\u64a4\u9000\u673a\u5236\u3002\u6765\u770b\u6700\u6838\u5fc3\u7684\u9632\u6b7b\u7b49\u903b\u8f91&#xff1a;<\/p>\n<p>Java<\/p>\n<p>public Object get(long timeoutMillis) {<br \/>\n    synchronized (lock) {<br \/>\n        long begin &#061; System.currentTimeMillis();<br \/>\n        long timePassed &#061; 0; \/\/ \u8bb0\u5f55\u5df2\u7ecf\u7ecf\u5386\u7684\u65f6\u95f4<\/p>\n<p>        while (response &#061;&#061; null) {<br \/>\n            \/\/ \u8ba1\u7b97\u8fd8\u5269\u591a\u5c11\u65f6\u95f4\u9700\u8981\u7b49<br \/>\n            long waitTime &#061; timeoutMillis &#8211; timePassed;<br \/>\n            if (waitTime &lt;&#061; 0) {<br \/>\n                break; \/\/ \u8d85\u65f6\u4e86&#xff0c;\u4e0d\u7b49\u4e86&#xff0c;\u76f4\u63a5\u6e9c<br \/>\n            }<br \/>\n            try {<br \/>\n                lock.wait(waitTime); \/\/ \u53ea\u7b49\u5269\u4f59\u7684\u65f6\u95f4<br \/>\n            } catch (InterruptedException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n            \/\/ \u9192\u6765\u540e&#xff08;\u53ef\u80fd\u662f\u88ab\u865a\u5047\u5524\u9192&#xff09;&#xff0c;\u91cd\u65b0\u8ba1\u7b97\u5df2\u7ecf\u7b49\u4e86\u591a\u4e45<br \/>\n            timePassed &#061; System.currentTimeMillis() &#8211; begin;<br \/>\n        }<br \/>\n        return response;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u6279\u8bed&#xff1a; \u8fd9\u91cc\u7684\u7cbe\u9ad3\u5728\u4e8e timePassed \u548c waitTime \u7684\u52a8\u6001\u8ba1\u7b97\u3002\u56e0\u4e3a\u7ebf\u7a0b\u5982\u679c\u4e2d\u9014\u88ab\u610f\u5916\u5524\u9192&#xff08;\u865a\u5047\u5524\u9192&#xff09;&#xff0c;\u518d\u8fdb\u5165 wait() \u65f6\u7edd\u5bf9\u4e0d\u80fd\u91cd\u65b0\u7b49 timeoutMillis \u90a3\u4e48\u4e45&#xff0c;\u5426\u5219\u8d85\u65f6\u673a\u5236\u5c31\u5f62\u540c\u865a\u8bbe\u4e86\u3002<\/p>\n<h5>&#x1f534; [\u7ea2\u706f\u533a] \u5f02\u6b65\u6a21\u5f0f\u4e4b\u751f\u4ea7\u8005-\u6d88\u8d39\u8005 (MessageQueue)<\/h5>\n<p>\u8fd9\u662f\u5b57\u8282\u8df3\u52a8\u7b49\u5934\u90e8\u5927\u5382\u6781\u9ad8\u9891\u7684\u73b0\u573a\u767d\u677f\/\u624b\u5199\u4ee3\u7801\u9898\u3002<\/p>\n<p>\u4fdd\u62a4\u6027\u6682\u505c\u662f\u201c\u4e00\u5bf9\u4e00\u201d\u7684\u540c\u6b65\u7b49\u5f85&#xff0c;\u4f46\u771f\u5b9e\u7684\u9ad8\u5e76\u53d1\u4e1a\u52a1&#xff08;\u6bd4\u5982\u5916\u5356\u7cfb\u7edf\u7684\u4e0b\u5355\u4e0e\u63a5\u5355&#xff09;\u5f80\u5f80\u662f\u201c\u591a\u5bf9\u591a\u201d\u7684\u5f02\u6b65\u5904\u7406\u3002\u4e2d\u5348\u9ad8\u5cf0\u671f\u5927\u91cf\u7528\u6237\u75af\u72c2\u4e0b\u5355&#xff08;\u751f\u4ea7\u8005&#xff09;&#xff0c;\u5982\u679c\u76f4\u63a5\u628a\u8bf7\u6c42\u7838\u7ed9\u6570\u636e\u5e93\u6216\u540e\u53a8\u6d3e\u5355\u7cfb\u7edf&#xff08;\u6d88\u8d39\u8005&#xff09;&#xff0c;\u7cfb\u7edf\u4e00\u79d2\u949f\u5c31\u5f97\u5d29\u3002<\/p>\n<p>\u6211\u4eec\u5fc5\u987b\u5728\u4e2d\u95f4\u52a0\u4e00\u4e2a\u201c\u5bb9\u91cf\u6709\u9650\u7684\u7f13\u51b2\u6c60\u201d&#xff0c;\u8fd9\u5c31\u662f\u963b\u585e\u961f\u5217&#xff08;MessageQueue&#xff09;\u3002\u751f\u4ea7\u8005\u53ea\u7ba1\u5f80\u91cc\u6254&#xff0c;\u6d88\u8d39\u8005\u53ea\u7ba1\u5f80\u5916\u62ff&#xff0c;\u5b8c\u7f8e\u89e3\u8026&#xff0c;\u524a\u5cf0\u586b\u8c37\u3002<\/p>\n<p>\u6781\u7b80\u6838\u5fc3\u4ee3\u7801&#xff1a;<\/p>\n<p>Java<\/p>\n<p>class MessageQueue {<br \/>\n    private LinkedList&lt;Message&gt; list &#061; new LinkedList&lt;&gt;();<br \/>\n    private int capacity; \/\/ \u5fc5\u987b\u6709\u5bb9\u91cf\u9650\u5236&#xff01;<\/p>\n<p>    public MessageQueue(int capacity) {<br \/>\n        this.capacity &#061; capacity;<br \/>\n    }<\/p>\n<p>    \/\/ \u6d88\u8d39\u8005\u62ff\u8d70\u6d88\u606f<br \/>\n    public Message take() {<br \/>\n        synchronized (list) {<br \/>\n            while (list.isEmpty()) { \/\/ \u6ca1\u6d88\u606f&#xff1f;\u6d88\u8d39\u8005\u7b49\u7740<br \/>\n                list.wait();<br \/>\n            }<br \/>\n            Message msg &#061; list.removeFirst();<br \/>\n            list.notifyAll(); \/\/ \u62ff\u8d70\u4e00\u4e2a&#xff0c;\u901a\u77e5\u751f\u4ea7\u8005\u53ef\u4ee5\u7ee7\u7eed\u653e\u4e86<br \/>\n            return msg;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u751f\u4ea7\u8005\u653e\u5165\u6d88\u606f<br \/>\n    public void put(Message message) {<br \/>\n        synchronized (list) {<br \/>\n            while (list.size() &#061;&#061; capacity) { \/\/ \u6ee1\u4e86&#xff1f;\u751f\u4ea7\u8005\u7b49\u7740<br \/>\n                list.wait();<br \/>\n            }<br \/>\n            list.addLast(message);<br \/>\n            list.notifyAll(); \/\/ \u653e\u8fdb\u4e00\u4e2a&#xff0c;\u901a\u77e5\u6d88\u8d39\u8005\u8d76\u7d27\u6765\u62ff<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>&#x1f4a1; \u5927\u5382\u9762\u8bd5\u95ee&#xff1a;<\/p>\n<p>\u9762\u8bd5\u5b98 1&#xff1a;\u5728 **take()** \u548c **put()** \u65b9\u6cd5\u4e2d&#xff0c;\u4e3a\u4ec0\u4e48\u7528 **while** \u5224\u65ad\u6761\u4ef6&#xff0c;\u800c\u4e0d\u662f **if**&#xff1f;\u7834\u5c40\u56de\u7b54&#xff1a; &#xff08;\u6b64\u5904\u8054\u52a8\u7b2c\u4e09\u7ae0&#xff09;\u4e3a\u4e86\u9632\u6b62\u865a\u5047\u5524\u9192\u3002\u4ee5 put \u4e3a\u4f8b&#xff0c;\u5982\u679c\u961f\u5217\u6ee1\u4e86&#xff0c;\u4e24\u4e2a\u751f\u4ea7\u8005 A \u548c B \u90fd\u5728 wait\u3002\u6b64\u65f6\u6d88\u8d39\u8005\u62ff\u8d70\u4e86\u4e00\u4e2a\u5143\u7d20\u5e76 notifyAll()&#xff0c;A \u548c B \u90fd\u9192\u4e86\u3002A \u62a2\u5230\u9501&#xff0c;\u585e\u5165\u4e00\u4e2a\u5143\u7d20&#xff0c;\u961f\u5217\u53c8\u6ee1\u4e86&#xff0c;\u7136\u540e A \u91ca\u653e\u9501\u3002\u5982\u679c\u4e0d\u7528 while \u5faa\u73af\u91cd\u65b0\u5224\u65ad&#xff0c;B \u63a5\u7740\u62ff\u5230\u9501\u540e\u4f1a\u76f4\u63a5\u5f80\u4e0b\u6267\u884c&#xff0c;\u5f3a\u884c\u5f80\u5df2\u7ecf\u6ee1\u4e86\u7684\u961f\u5217\u91cc\u585e\u6570\u636e&#xff0c;\u76f4\u63a5\u5bfc\u81f4\u8d8a\u754c\u62a5\u9519\u3002<\/p>\n<p>**\u9762\u8bd5\u5b98 2&#xff1a;\u6700\u540e\u7684\u5524\u9192\u4e3a\u4ec0\u4e48\u7528 **notifyAll()**&#xff0c;\u6539\u6210 **notify()** \u4f1a\u6b7b\u9501\u5417&#xff1f;**\u7834\u5c40\u56de\u7b54&#xff1a; \u7edd\u5bf9\u4f1a\u6b7b\u9501&#xff01;\u5047\u8bbe\u961f\u5217\u6ee1\u4e86&#xff0c;\u4e24\u4e2a\u751f\u4ea7\u8005\u5728\u7b49\u3002\u4e00\u4e2a\u6d88\u8d39\u8005\u62ff\u8d70\u4e86\u4e00\u4e2a\u6570\u636e&#xff0c;\u6b64\u65f6\u4ed6\u5982\u679c\u53ea\u7528 notify()&#xff0c;\u6070\u597d\u5524\u9192\u4e86\u53e6\u4e00\u4e2a\u6d88\u8d39\u8005&#xff08;\u540c\u7c7b\u5524\u9192\u540c\u7c7b&#xff09;&#xff0c;\u8fd9\u4e2a\u88ab\u5524\u9192\u7684\u6d88\u8d39\u8005\u53d1\u73b0\u961f\u5217\u7a7a\u4e86&#xff0c;\u53c8\u7761\u8fc7\u53bb\u4e86\u3002\u6b64\u65f6\u751f\u4ea7\u8005\u6839\u672c\u6ca1\u6536\u5230\u4fe1\u53f7&#xff0c;\u6240\u6709\u4eba\u90fd\u7761\u6b7b\u8fc7\u53bb\u4e86\u3002\u5fc5\u987b\u7528 notifyAll() \u4fdd\u8bc1\u81f3\u5c11\u6709\u4e00\u4e2a\u5bf9\u7acb\u9635\u8425\u7684\u7ebf\u7a0b\u88ab\u5524\u9192\u3002<\/p>\n<p>&#x1f6e0;\ufe0f \u771f\u5b9e\u4e1a\u52a1\u9632\u5751\u6307\u5357&#xff08;\u5916\u5356\u9ad8\u5e76\u53d1\u573a\u666f&#xff09;&#xff1a; \u5728\u5f00\u53d1\u5916\u5356\u4e1a\u52a1\u7684\u8ba2\u5355\u5206\u53d1\u6a21\u5757\u65f6&#xff0c;\u5f88\u591a\u4eba\u4e3a\u4e86\u56fe\u7701\u4e8b&#xff0c;\u76f4\u63a5\u7528 new LinkedList&lt;&gt;() \u6216\u8005\u65e0\u754c\u961f\u5217&#xff08;\u5982\u9ed8\u8ba4\u7684 LinkedBlockingQueue&#xff09;\u505a\u5f02\u6b65\u3002\u4e00\u65e6\u5230\u4e86\u996d\u70b9&#xff0c;\u77ac\u95f4\u6d8c\u5165\u51e0\u4e07\u4e2a\u8ba2\u5355&#xff0c;\u800c\u6d3e\u5355\u7cfb\u7edf\u7684\u6d88\u8d39\u80fd\u529b\u6bcf\u79d2\u53ea\u6709\u51e0\u767e\u4e2a&#xff0c;\u8ba2\u5355\u5c31\u4f1a\u5728\u5185\u5b58\u961f\u5217\u91cc\u75af\u72c2\u5806\u79ef\u3002\u6700\u7ec8\u7ed3\u679c\u5fc5\u7136\u662f OOM&#xff08;\u5185\u5b58\u6ea2\u51fa&#xff09;&#xff0c;\u6574\u4e2a\u5916\u5356\u670d\u52a1\u5b95\u673a\u62a5\u9519\u3002 \u6240\u4ee5&#xff0c;\u5de5\u4e1a\u7ea7\u7684\u6d88\u606f\u961f\u5217\u5fc5\u987b\u8bbe\u7f6e **capacity** \u4e0a\u9650&#xff08;\u5982\u54b1\u4eec\u4ee3\u7801\u4e2d\u7684 while (list.size() &#061;&#061; capacity)&#xff09;&#xff0c;\u5f53\u961f\u5217\u6ee1\u4e86\u65f6&#xff0c;\u5b81\u53ef\u963b\u585e\u8bf7\u6c42\u6216\u8005\u5feb\u901f\u5931\u8d25\u629b\u51fa\u201c\u7cfb\u7edf\u7e41\u5fd9\u8bf7\u91cd\u8bd5\u201d&#xff0c;\u4e5f\u7edd\u4e0d\u80fd\u628a\u5185\u5b58\u6491\u7206\u3002<\/p>\n<h4>\u4e94\u3001\u6d3b\u8dc3\u6027\u707e\u96be\u4e0e\u6392\u67e5\u6307\u5357<\/h4>\n<h5>&#x1f534;_ [\u7ea2\u706f\u533a] \u4ea7\u751f\u6b7b\u9501\u4e0e Linux \u7ebf\u4e0a\u6392\u67e5\u5b9a\u4f4d_<\/h5>\n<p>\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u4e92\u76f8\u6301\u6709\u5bf9\u65b9\u9700\u8981\u7684\u9501&#xff0c;\u5e76\u4e14\u90fd\u5728\u6b7b\u7b49\u5bf9\u65b9\u91ca\u653e\u65f6&#xff0c;\u7a0b\u5e8f\u5c31\u5f7b\u5e95\u50f5\u6b7b\u4e86\u3002\u8fd9\u5c31\u662f\u6700\u81f4\u547d\u7684\u6d3b\u8dc3\u6027\u707e\u96be&#xff1a;\u6b7b\u9501&#xff08;Deadlock&#xff09;\u3002<\/p>\n<p>\u4e0d\u8981\u53bb\u6b7b\u8bb0\u786c\u80cc\u6559\u79d1\u4e66\u91cc\u90a3\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6&#xff08;\u4e92\u65a5\u3001\u4e0d\u53ef\u5265\u593a\u3001\u8bf7\u6c42\u4e0e\u4fdd\u6301\u3001\u5faa\u73af\u7b49\u5f85&#xff09;&#xff0c;\u5927\u5382\u9762\u8bd5\u5b98\u548c\u7ebf\u4e0a\u771f\u5b9e\u73af\u5883\u6839\u672c\u4e0d\u5403\u8fd9\u4e00\u5957\u3002\u4ed6\u4eec\u53ea\u770b\u4e24\u70b9&#xff1a;\u4f60\u600e\u4e48\u5199\u51fa\u6b7b\u9501\u7684&#xff1f;\u4f60\u600e\u4e48\u5728 Linux \u9ed1\u6846\u6846\u91cc\u628a\u5b83\u63ea\u51fa\u6765\u7684&#xff1f;<\/p>\n<p>\u6781\u7b80\u6b7b\u9501\u4ee3\u7801\u518d\u73b0&#xff1a;<\/p>\n<p>Java<\/p>\n<p>public void badCode() {<br \/>\n    Object lockA &#061; new Object();<br \/>\n    Object lockB &#061; new Object();<\/p>\n<p>    new Thread(() -&gt; {<br \/>\n        synchronized (lockA) {<br \/>\n            \/\/ \u62ff\u5230 A&#xff0c;\u60f3\u8981 B<br \/>\n            try { Thread.sleep(100); } catch (Exception e) {}<br \/>\n            synchronized (lockB) { System.out.println(&#034;T1 \u6267\u884c&#034;); }<br \/>\n        }<br \/>\n    }, &#034;Thread-1&#034;).start();<\/p>\n<p>    new Thread(() -&gt; {<br \/>\n        synchronized (lockB) {<br \/>\n            \/\/ \u62ff\u5230 B&#xff0c;\u60f3\u8981 A<br \/>\n            try { Thread.sleep(100); } catch (Exception e) {}<br \/>\n            synchronized (lockA) { System.out.println(&#034;T2 \u6267\u884c&#034;); }<br \/>\n        }<br \/>\n    }, &#034;Thread-2&#034;).start();<br \/>\n}<\/p>\n<p>&#x1f6e0;\ufe0f_ _\u5927\u5382\u7ebf\u4e0a\u6392\u67e5\u5b9e\u6218&#xff08;\u6392\u67e5\u94fe\u8def&#xff09;&#xff1a;<\/p>\n<p>\u5047\u8bbe\u4f60\u8d1f\u8d23\u7684\u7cfb\u7edf\u7a81\u7136 CPU \u98d9\u9ad8&#xff0c;\u6216\u8005\u90e8\u5206\u63a5\u53e3\u4e00\u76f4\u8d85\u65f6\u8f6c\u5708\u5708&#xff0c;\u5982\u4f55\u5728\u6ca1\u6709\u754c\u9762\u7684 Linux \u670d\u52a1\u5668\u4e0a\u5b9a\u4f4d&#xff1f;<\/p>\n<p>\u7b2c\u4e00\u6b65&#xff1a;\u627e\u5185\u9b3c\u8fdb\u7a0b (**top**)_ \u8f93\u5165 _top_ \u547d\u4ee4&#xff0c;\u6309\u5927\u5199 _P_ \u6839\u636e CPU \u5360\u7528\u6392\u5e8f&#xff0c;\u6216\u8005\u51ed\u4e1a\u52a1\u7aef\u53e3\u627e\u5230\u4f60\u7684 Java \u8fdb\u7a0b PID&#xff08;\u5047\u8bbe\u662f 10086&#xff09;\u3002_<\/p>\n<p>\u7b2c\u4e8c\u6b65&#xff1a;\u63ea\u51fa\u4f5c\u5996\u7ebf\u7a0b (**top -Hp 10086**)_ \u67e5\u770b\u8fd9\u4e2a\u8fdb\u7a0b\u91cc\u5230\u5e95\u662f\u54ea\u4e2a\u7ebf\u7a0b\u5728\u641e\u9b3c\u3002\u4f60\u4f1a\u770b\u5230\u51e0\u4e2a CPU \u5360\u7528\u6781\u9ad8&#xff0c;\u6216\u8005\u5904\u4e8e\u957f\u65f6\u95f4 Block \u7684\u7ebf\u7a0b ID&#xff08;TID&#xff0c;\u5047\u8bbe\u662f 10090&#xff09;\u3002\u6b64\u65f6&#xff0c;\u5c06\u8fd9\u4e2a\u5341\u8fdb\u5236\u7684 10090 \u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236&#xff08;\u901a\u5e38\u7528\u547d\u4ee4 _printf &#034;%x\\\\n&#034; 10090_&#xff09;&#xff0c;\u5f97\u5230 _276a_\u3002_<\/p>\n<p>\u7b2c\u4e09\u6b65&#xff1a;\u62cd\u4e0b\u51f6\u6848\u73b0\u573a (**jstack 10086 &gt; thread_dump.txt**)_ \u5bfc\u51fa\u5f53\u524d\u7684\u7ebf\u7a0b\u6808\u5feb\u7167\u3002\u7136\u540e\u53bb\u6587\u4ef6\u91cc\u641c\u7d22\u5341\u516d\u8fdb\u5236\u7684 _276a_&#xff0c;\u770b\u770b\u5b83\u5230\u5e95\u505c\u5728\u4ee3\u7801\u7684\u54ea\u4e00\u884c&#xff01; \u66f4\u76f4\u63a5\u7684&#xff0c;\u76f4\u63a5\u641c\u7d22 JDK \u7ed9\u4f60\u603b\u7ed3\u597d\u7684\u9b54\u6cd5\u5173\u952e\u5b57&#xff1a;_Found one Java-level deadlock:_\u3002\u53ea\u8981\u770b\u5230\u8fd9\u4e2a\u8bcd&#xff0c;\u5b83\u4f1a\u6781\u5176\u76f4\u767d\u5730\u544a\u8bc9\u4f60&#xff1a;Thread-1 \u9501\u4f4f\u4e86\u5bf9\u8c61 X \u6b63\u5728\u7b49\u5bf9\u8c61 Y&#xff0c;\u800c Thread-2 \u9501\u4f4f\u4e86\u5bf9\u8c61 Y \u6b63\u5728\u7b49\u5bf9\u8c61 X&#xff0c;\u8fde\u4ee3\u7801\u884c\u53f7\u90fd\u7ed9\u4f60\u6807\u5f97\u6e05\u6e05\u695a\u695a\u3002_<\/p>\n<p>&#x1f4a1;_ \u771f\u5b9e\u4e1a\u52a1\u9632\u5751\u6307\u5357&#xff08;\u7ed3\u5408\u82cd\u7a79\u5916\u5356\u573a\u666f&#xff09;&#xff1a;****\u73b0\u8c61&#xff1a; \u5728\u5f00\u53d1\u5916\u5356\u7cfb\u7edf\u7684\u9a91\u624b\u7aef\u65f6&#xff0c;\u6709\u4e00\u4e2a\u9690\u85cf\u6781\u6df1\u7684 Bug\u3002\u9a91\u624b A \u5e2e\u9a91\u624b B \u4ee3\u9001\u4e00\u5355&#xff0c;\u7cfb\u7edf\u5e95\u5c42\u53d1\u751f\u8d26\u6237\u8d44\u91d1\u8f6c\u8d26\u3002 \u4ee3\u7801\u903b\u8f91\u5982\u679c\u662f&#xff1a;_synchronized(A\u7684\u8d26\u6237) { synchronized(B\u7684\u8d26\u6237) { \u6267\u884c\u8f6c\u8d26 } }_\u3002 \u707e\u96be\u7206\u53d1&#xff1a; \u5047\u5982\u67d0\u4e00\u65f6\u523b&#xff0c;A \u7ed9 B \u8f6c\u8d26&#xff0c;\u540c\u65f6 B \u4e5f\u7ed9 A \u8f6c\u8d26\u3002\u7ebf\u7a0b 1 \u9501\u4f4f\u4e86 A \u7b49 B&#xff0c;\u7ebf\u7a0b 2 \u9501\u4f4f\u4e86 B \u7b49 A\u3002\u4e00\u65e6\u4e24\u4e2a\u8bf7\u6c42\u5728\u540c\u4e00\u6beb\u79d2\u5e76\u53d1&#xff0c;\u7cfb\u7edf\u7684\u8fd9\u5757\u8f6c\u8d26\u4e1a\u52a1\u5c31\u6c38\u4e45\u5b95\u673a\u4e86\u3002 \u7834\u5c40\u89e3\u6cd5&#xff08;\u9501\u6392\u5e8f&#xff09;&#xff1a; \u6253\u7834\u5faa\u73af\u7b49\u5f85\u6761\u4ef6\u3002\u65e0\u8bba\u8c01\u7ed9\u8c01\u8f6c\u8d26&#xff0c;\u5728\u52a0\u9501\u4e4b\u524d&#xff0c;\u5148\u5224\u65ad\u8d26\u6237 ID \u7684\u5927\u5c0f\u3002\u6c38\u8fdc\u5f3a\u5236\u5148\u9501 ID \u5c0f\u7684\u8d26\u6237&#xff0c;\u518d\u9501 ID \u5927\u7684\u8d26\u6237\u3002 \u8fd9\u6837\u6240\u6709\u7ebf\u7a0b\u83b7\u53d6\u9501\u7684\u987a\u5e8f\u5c31\u4fdd\u6301\u4e00\u81f4\u4e86&#xff0c;\u6b7b\u9501\u4e0d\u653b\u81ea\u7834&#xff01;_<\/p>\n<h5>&#x1f7e2;_ [\u7eff\u706f\u533a] \u6d3b\u9501\u4e0e\u9965\u997f&#xff1a;\u90a3\u4e9b\u6ca1\u90a3\u4e48\u81f4\u547d\u7684\u9690\u60a3_<\/h5>\n<p>\u76f8\u6bd4\u4e8e\u6b7b\u9501\u7684\u201c\u5f7b\u5e95\u50f5\u6b7b\u201d&#xff0c;\u8fd8\u6709\u4e24\u79cd\u6d3b\u8dc3\u6027\u95ee\u9898&#xff0c;\u53ea\u9700\u4e86\u89e3\u6982\u5ff5\u5373\u53ef\u3002<\/p>\n<ul>\n<li>\u6d3b\u9501 (Livelock)&#xff1a;_ \u7ebf\u7a0b\u5e76\u6ca1\u6709\u88ab\u963b\u585e&#xff0c;\u800c\u662f\u5728\u4e0d\u505c\u5730\u6539\u53d8\u72b6\u6001&#xff0c;\u4f46\u4e1a\u52a1\u8fdb\u5ea6\u5c31\u662f\u63a8\u4e0d\u4e0b\u53bb\u3002 \u8fd9\u5c31\u597d\u6bd4\u5e73\u65f6\u54b1\u4eec\u559c\u6b22\u6363\u9f13\u7684\u7279\u8c03\u5496\u5561&#xff0c;\u5047\u8bbe\u4f60\u8981\u505a\u4e00\u676f\u201c\u6a59 C \u7f8e\u5f0f\u201d&#xff0c;\u9700\u8981\u7528\u5230\u6d53\u7f29\u6db2\u548c\u6a59\u6c41\u3002\u4f60\u548c\u90a3\u4e2a\u5e73\u65f6\u6709\u70b9\u6bdb\u8e81\u7684\u5ba4\u53cb\u540c\u65f6\u5728\u53a8\u623f&#xff0c;\u4f60\u62ff\u5230\u4e86\u6d53\u7f29\u6db2&#xff0c;\u4ed6\u62ff\u5230\u4e86\u6a59\u6c41\u3002\u4e3a\u4e86\u4e0d\u5361\u6b7b&#xff0c;\u4f60\u4eec\u4fe9\u90fd\u975e\u5e38\u6709\u793c\u8c8c\u5730\u628a\u624b\u91cc\u7684\u4e1c\u897f\u653e\u4e0b\u8ba9\u7ed9\u5bf9\u65b9&#xff1b;\u7ed3\u679c\u4e0b\u4e00\u79d2&#xff0c;\u4fe9\u4eba\u53c8\u540c\u65f6\u62ff\u8d77\u4e86\u539f\u5148\u7684\u4e1c\u897f\u3002\u5927\u5bb6\u90fd\u5728\u75af\u72c2\u5730\u201c\u8ba9\u6b65 &#8211; \u5c1d\u8bd5 &#8211; \u8ba9\u6b65\u201d&#xff0c;CPU \u75af\u72c2\u7a7a\u8f6c&#xff0c;\u4f46\u8c01\u4e5f\u6ca1\u559d\u4e0a\u4e00\u53e3\u5496\u5561\u3002 _(\u89e3\u6cd5&#xff1a;\u5f15\u5165\u968f\u673a\u7761\u7720\u65f6\u95f4&#xff0c;\u6253\u7834\u8fd9\u79cd\u6781\u5c0f\u6982\u7387\u7684\u7edd\u5bf9\u540c\u6b65)<\/li>\n<li>\u9965\u997f (Starvation)&#xff1a;_ \u67d0\u4e2a\u5012\u9709\u7684\u7ebf\u7a0b\u56e0\u4e3a\u4f18\u5148\u7ea7\u592a\u4f4e&#xff0c;\u6216\u8005\u5176\u4ed6\u7ebf\u7a0b\u592a\u5f3a\u52bf&#xff08;\u6bd4\u5982\u975e\u516c\u5e73\u9501\u91cc\u522b\u4eba\u4e00\u76f4\u63d2\u961f&#xff09;&#xff0c;\u5bfc\u81f4\u5b83\u6c38\u8fdc\u62a2\u4e0d\u5230 CPU \u8c03\u5ea6&#xff0c;\u53ea\u80fd\u5728\u89d2\u843d\u91cc\u753b\u5708\u5708\u3002 (\u89e3\u6cd5&#xff1a;\u5c3d\u91cf\u4f7f\u7528\u516c\u5e73\u9501&#xff0c;\u6216\u8005\u907f\u514d\u4eba\u4e3a\u968f\u610f\u8c03\u6574\u7ebf\u7a0b\u7684 Priority \u4f18\u5148\u7ea7)_<\/li>\n<\/ul>\n<hr \/>\n<h4>\u516d\u3001\u603b\u7ed3<\/h4>\n<p>\u4eca\u5929\u6211\u4eec\u62c6\u89e3\u4e86 Monitor \u7ba1\u7a0b\u3001\u5256\u6790\u4e86\u7ebf\u7a0b\u534f\u4f5c&#xff08;Wait\/Notify \u4e0e LockSupport \u5e95\u5c42\u673a\u5236&#xff09;&#xff0c;\u5e76\u624b\u5199\u4e86\u963b\u585e\u961f\u5217\u4e0e\u6b7b\u9501\u6392\u67e5\u3002\u8fd9\u5e26\u7ed9\u6211\u4eec\u7684\u6838\u5fc3\u6307\u5bfc\u610f\u4e49\u662f&#xff1a;\u5e76\u53d1\u4ece\u6765\u4e0d\u662f\u65e0\u8111\u52a0\u9501\u7684\u7384\u5b66&#xff0c;\u800c\u662f\u4e25\u8c28\u7684\u8d44\u6e90\u8c03\u5ea6\u827a\u672f\u3002 \u53ea\u6709\u770b\u900f\u5e95\u5c42&#xff0c;\u4f60\u624d\u80fd\u5728\u9047\u5230\u7cfb\u7edf\u5047\u6b7b\u65f6\u8fc5\u901f\u7528 jstack \u63ea\u51fa\u5185\u9b3c&#xff0c;\u624d\u80fd\u9488\u5bf9\u4e1a\u52a1\u7279\u70b9\u7cbe\u51c6\u8bbe\u8ba1\u540c\u6b65\u6a21\u5f0f&#xff0c;\u771f\u6b63\u505a\u5230\u5bf9\u7cfb\u7edf\u8d44\u6e90\u7684\u656c\u754f\u4e0e\u628a\u63a7\u3002<\/p>\n<p>\u7559\u4e0b\u60ac\u5ff5&#xff1a;\u62ff\u5230\u9501&#xff0c;\u6570\u636e\u5c31\u7edd\u5bf9\u5b89\u5168\u4e86\u5417&#xff1f;<\/p>\n<p>\u901a\u8fc7\u8fd9\u7bc7\u535a\u5ba2&#xff0c;\u6211\u4eec\u5229\u7528\u9501\u673a\u5236\u89e3\u51b3\u4e86\u591a\u7ebf\u7a0b\u201c\u6392\u961f\u201d\u6267\u884c\u7684\u95ee\u9898&#xff0c;\u5b8c\u7f8e\u62ff\u4e0b\u4e86\u5e76\u53d1\u4e09\u5927\u7279\u6027\u4e2d\u7684**\u201c\u539f\u5b50\u6027\u201d**\u3002\u4f46\u662f&#xff0c;\u5e76\u53d1\u4e16\u754c\u7684\u5e7d\u7075\u5e76\u672a\u5c31\u6b64\u6d88\u6563&#xff1a;<\/p>\n<p>\u5047\u8bbe\u5728\u591a\u6838 CPU \u67b6\u6784\u4e0b&#xff0c;\u7ebf\u7a0b A \u521a\u521a\u4fee\u6539\u4e86\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf&#xff0c;\u7ebf\u7a0b B \u5374\u56e0\u4e3a CPU \u9ad8\u901f\u7f13\u5b58\u7684\u5b58\u5728&#xff0c;\u6b7b\u6d3b\u8bfb\u4e0d\u5230\u6700\u65b0\u6570\u636e&#xff08;\u53ef\u89c1\u6027\u5931\u6548&#xff09;&#xff01;\u66f4\u53ef\u6015\u7684\u662f&#xff0c;JVM \u4e3a\u4e86\u6781\u81f4\u69a8\u5e72\u786c\u4ef6\u6027\u80fd&#xff0c;\u7adf\u7136\u4f1a\u5077\u5077\u628a\u4f60\u5199\u7684\u4ee3\u7801\u6307\u4ee4\u987a\u5e8f\u6253\u4e71&#xff08;\u6307\u4ee4\u91cd\u6392&#xff09;&#xff01;<\/p>\n<p>\u4e0b\u4e00\u7bc7\u6587\u7ae0**\u300a\u6df1\u5165\u7406\u89e3 JMM&#xff1a;Volatile \u5173\u952e\u5b57\u4e0e Synchronized \u9501\u5347\u7ea7\u4e4b\u8def\u300b&#xff0c;\u6211\u4eec\u5c06\u8e0f\u5165\u5e76\u53d1\u7684\u53e6\u4e00\u5927\u6df1\u6c34\u533a\u3002\u6211\u5c06\u5e26\u4f60\u624b\u6495 JMM&#xff08;Java \u5185\u5b58\u6a21\u578b&#xff09;&#xff0c;\u63ed\u5f00 **volatile** \u5173\u952e\u5b57\u80cc\u540e\u786c\u6838\u7684\u5185\u5b58\u5c4f\u969c**\u539f\u7406&#xff0c;\u5e76\u5e26\u4f60\u624b\u5199\u5927\u5382\u5fc5\u8003\u7684 DCL&#xff08;\u53cc\u91cd\u68c0\u67e5\u9501&#xff09;\u5355\u4f8b\u6a21\u5f0f\u4e0e\u5e95\u5c42 Happens-Before \u89c4\u5219\u3002\u6211\u4eec\u4e0b\u671f\u89c1&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00&#xff1a;\u201c\u5728\u5355\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u4ee3\u7801\u7684\u6267\u884c\u5c31\u50cf\u4e00\u6761\u7b14\u76f4\u7684\u9ad8\u901f\u516c\u8def&#xff0c;\u53ef\u9884\u6d4b\u4e14\u5b89\u5168\u3002\u4f46\u5f53\u6211\u4eec\u8e0f\u5165\u591a\u7ebf\u7a0b\u7684\u9886\u5730&#xff0c;\u5171\u4eab\u8d44\u6e90\u5c31\u6210\u4e86\u4e00\u4e2a\u5371\u673a\u56db\u4f0f\u7684\u5341\u5b57\u8def\u53e3\u3002\u5982\u679c\u6ca1\u6709\u7ea2\u7eff\u706f&#xff08;\u540c\u6b65\u673a\u5236&#xff09;&#xff0c;\u8f66\u8f86&#xff08;\u7ebf\u7a0b&#xff09;\u7684\u6a2a\u51b2\u76f4\u649e\u5fc5\u7136\u5bfc\u81f4\u8f66\u7978&#xff08;\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff09;\u3002<br \/>\n\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u6700\u57fa\u672c\u7684\u7ebf\u7a0b\u5b89\u5168\u73b0\u8c61\u5165\u624b&#xff0c;\u4e00\u8def\u5411\u4e0b\u786c\u6838\u5256\u6790 JVM \u662f\u5982\u4f55\u901a\u8fc7 Synchron<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,426,188],"topic":[],"class_list":["post-78853","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-jvm","tag-188"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/78853.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f15\u8a00&#xff1a;\u201c\u5728\u5355\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u4ee3\u7801\u7684\u6267\u884c\u5c31\u50cf\u4e00\u6761\u7b14\u76f4\u7684\u9ad8\u901f\u516c\u8def&#xff0c;\u53ef\u9884\u6d4b\u4e14\u5b89\u5168\u3002\u4f46\u5f53\u6211\u4eec\u8e0f\u5165\u591a\u7ebf\u7a0b\u7684\u9886\u5730&#xff0c;\u5171\u4eab\u8d44\u6e90\u5c31\u6210\u4e86\u4e00\u4e2a\u5371\u673a\u56db\u4f0f\u7684\u5341\u5b57\u8def\u53e3\u3002\u5982\u679c\u6ca1\u6709\u7ea2\u7eff\u706f&#xff08;\u540c\u6b65\u673a\u5236&#xff09;&#xff0c;\u8f66\u8f86&#xff08;\u7ebf\u7a0b&#xff09;\u7684\u6a2a\u51b2\u76f4\u649e\u5fc5\u7136\u5bfc\u81f4\u8f66\u7978&#xff08;\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff09;\u3002 \u672c\u6587\u5c06\u5e26\u4f60\u4ece\u6700\u57fa\u672c\u7684\u7ebf\u7a0b\u5b89\u5168\u73b0\u8c61\u5165\u624b&#xff0c;\u4e00\u8def\u5411\u4e0b\u786c\u6838\u5256\u6790 JVM \u662f\u5982\u4f55\u901a\u8fc7 Synchron\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78853.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-28T04:57:07+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78853.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78853.html\",\"name\":\"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-28T04:57:07+00:00\",\"dateModified\":\"2026-02-28T04:57:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78853.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78853.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78853.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/78853.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f15\u8a00&#xff1a;\u201c\u5728\u5355\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u4ee3\u7801\u7684\u6267\u884c\u5c31\u50cf\u4e00\u6761\u7b14\u76f4\u7684\u9ad8\u901f\u516c\u8def&#xff0c;\u53ef\u9884\u6d4b\u4e14\u5b89\u5168\u3002\u4f46\u5f53\u6211\u4eec\u8e0f\u5165\u591a\u7ebf\u7a0b\u7684\u9886\u5730&#xff0c;\u5171\u4eab\u8d44\u6e90\u5c31\u6210\u4e86\u4e00\u4e2a\u5371\u673a\u56db\u4f0f\u7684\u5341\u5b57\u8def\u53e3\u3002\u5982\u679c\u6ca1\u6709\u7ea2\u7eff\u706f&#xff08;\u540c\u6b65\u673a\u5236&#xff09;&#xff0c;\u8f66\u8f86&#xff08;\u7ebf\u7a0b&#xff09;\u7684\u6a2a\u51b2\u76f4\u649e\u5fc5\u7136\u5bfc\u81f4\u8f66\u7978&#xff08;\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff09;\u3002 \u672c\u6587\u5c06\u5e26\u4f60\u4ece\u6700\u57fa\u672c\u7684\u7ebf\u7a0b\u5b89\u5168\u73b0\u8c61\u5165\u624b&#xff0c;\u4e00\u8def\u5411\u4e0b\u786c\u6838\u5256\u6790 JVM \u662f\u5982\u4f55\u901a\u8fc7 Synchron","og_url":"https:\/\/www.wsisp.com\/helps\/78853.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-28T04:57:07+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78853.html","url":"https:\/\/www.wsisp.com\/helps\/78853.html","name":"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-28T04:57:07+00:00","dateModified":"2026-02-28T04:57:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78853.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78853.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78853.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300cJUC\u300d\u7ebf\u7a0b\u5b89\u5168\u4e0e\u7ba1\u7a0b\u673a\u5236\uff1aSynchronized \u539f\u7406\u4e0e\u7b49\u5f85\u5524\u9192\u673a\u5236"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=78853"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78853\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78853"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}