{"id":3917,"date":"2025-07-20T10:37:37","date_gmt":"2025-07-20T02:37:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3917"},"modified":"2025-07-20T10:37:37","modified_gmt":"2025-07-20T02:37:37","slug":"%e4%bb%80%e4%b9%88%e6%98%af%e5%8f%af%e9%87%8d%e5%85%a5%ef%bc%8c%e4%bb%80%e4%b9%88%e6%98%af%e5%8f%af%e9%87%8d%e5%85%a5%e9%94%81-%e5%ae%83%e7%94%a8%e6%9d%a5%e8%a7%a3%e5%86%b3%e4%bb%80%e4%b9%88%e9%97%ae","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/20\/%e4%bb%80%e4%b9%88%e6%98%af%e5%8f%af%e9%87%8d%e5%85%a5%ef%bc%8c%e4%bb%80%e4%b9%88%e6%98%af%e5%8f%af%e9%87%8d%e5%85%a5%e9%94%81-%e5%ae%83%e7%94%a8%e6%9d%a5%e8%a7%a3%e5%86%b3%e4%bb%80%e4%b9%88%e9%97%ae\/","title":{"rendered":"\u4ec0\u4e48\u662f\u53ef\u91cd\u5165\uff0c\u4ec0\u4e48\u662f\u53ef\u91cd\u5165\u9501? \u5b83\u7528\u6765\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898?"},"content":{"rendered":"\n<p>\u5728 Java \u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c<strong>\u201c\u53ef\u91cd\u5165\u201d<\/strong> \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5b83\u5173\u7cfb\u5230\u7ebf\u7a0b\u5982\u4f55\u83b7\u53d6\u9501\uff0c\u662f\u5426\u5141\u8bb8<strong>\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u8fdb\u5165\u540c\u4e00\u4e2a\u9501\u4fdd\u62a4\u7684\u4ee3\u7801\u5757<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f\u53ef\u91cd\u5165\uff08Reentrancy\uff09\uff1f<\/h2>\n\n\n\n<p><strong>\u53ef\u91cd\u5165\uff08Reentrancy\uff09<\/strong>\uff0c\u53c8\u79f0\u201c\u9012\u5f52\u9501\u201d\uff0c\u662f\u6307\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5916\u5c42\u65b9\u6cd5\u83b7\u5f97\u9501\u4e4b\u540e\uff0c\u5728\u8fdb\u5165\u5185\u5c42\u65b9\u6cd5\u65f6<\/strong>\uff0c<strong>\u5982\u679c\u5c1d\u8bd5\u83b7\u53d6\u540c\u4e00\u4e2a\u9501\uff0c\u4ecd\u7136\u80fd\u591f\u83b7\u53d6\u6210\u529f<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b80\u5355\u7406\u89e3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u5df1\u53ef\u4ee5\u518d\u6b21\u8fdb\u5165\u81ea\u5df1\u5df2\u7ecf\u62e5\u6709\u7684\u9501\uff0c\u800c\u4e0d\u4f1a\u9020\u6210\u6b7b\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4ec0\u4e48\u662f\u53ef\u91cd\u5165\u9501\uff08Reentrant Lock\uff09\uff1f<\/h2>\n\n\n\n<p><strong>\u53ef\u91cd\u5165\u9501<\/strong>\u662f\u4e00\u79cd<strong>\u5141\u8bb8\u540c\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u83b7\u5f97\u540c\u4e00\u628a\u9501\u800c\u4e0d\u4f1a\u88ab\u963b\u585e<\/strong>\u7684\u9501\u3002Java \u4e2d\u6700\u5e38\u89c1\u7684\u53ef\u91cd\u5165\u9501\u6709\u4e24\u4e2a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53ef\u91cd\u5165\u9501<\/th><th>\u6240\u5728\u5305<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td><code>synchronized<\/code><\/td><td>Java \u8bed\u8a00\u5185\u7f6e\u5173\u952e\u5b57<\/td><td>\u53ef\u91cd\u5165\uff0c\u81ea\u52a8\u7ba1\u7406\u91cd\u5165\u8ba1\u6570<\/td><\/tr><tr><td><code>ReentrantLock<\/code><\/td><td><code>java.util.concurrent.locks<\/code><\/td><td>\u53ef\u91cd\u5165\uff0c\u652f\u6301\u53ef\u4e2d\u65ad\u3001\u516c\u5e73\u9501\u3001\u5c1d\u8bd5\u9501\u7b49\u7279\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u53ef\u91cd\u5165\u9501\u7684\u4ee3\u7801\u6f14\u793a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<code>synchronized<\/code> \u7684\u53ef\u91cd\u5165\u6027<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ReentrantExample {\n    public synchronized void outer() {\n        System.out.println(\"\u5916\u5c42\u65b9\u6cd5\");\n        inner();  \/\/ \u5f53\u524d\u7ebf\u7a0b\u518d\u6b21\u8fdb\u5165 synchronized \u65b9\u6cd5\n    }\n\n    public synchronized void inner() {\n        System.out.println(\"\u5185\u5c42\u65b9\u6cd5\");\n    }\n\n    public static void main(String&#91;] args) {\n        new ReentrantExample().outer();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5916\u5c42\u65b9\u6cd5\n\u5185\u5c42\u65b9\u6cd5<\/code><\/pre>\n\n\n\n<p>\u5373\uff1a<strong>\u4e00\u4e2a\u7ebf\u7a0b\u5df2\u7ecf\u6301\u6709\u4e86\u67d0\u5bf9\u8c61\u7684\u9501\uff0c\u8fd8\u53ef\u4ee5\u518d\u6b21\u8fdb\u5165\u8be5\u5bf9\u8c61\u7684 synchronized \u65b9\u6cd5<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001ReentrantLock \u7684\u91cd\u5165\u7279\u6027<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ReentrantLock lock = new ReentrantLock();\n\npublic void outer() {\n    lock.lock();\n    try {\n        System.out.println(\"outer\");\n        inner();\n    } finally {\n        lock.unlock();\n    }\n}\n\npublic void inner() {\n    lock.lock();\n    try {\n        System.out.println(\"inner\");\n    } finally {\n        lock.unlock();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>outer\ninner<\/code><\/pre>\n\n\n\n<p><strong>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u8c03\u7528 <code>lock()<\/code>\uff0c\u4e0d\u4f1a\u6b7b\u9501\uff0c\u4f46\u9700\u8981\u624b\u52a8\u8c03\u7528\u591a\u6b21 <code>unlock()<\/code>\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u53ef\u91cd\u5165\u9501\u89e3\u51b3\u4e86\u4ec0\u4e48\u95ee\u9898\uff1f<\/h2>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u53ef\u91cd\u5165\u7279\u6027\uff0c\u4ee3\u7801\u4e2d\u4f1a\u5bb9\u6613\u51fa\u73b0<strong>\u6b7b\u9501\u95ee\u9898<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5047\u8bbe\u9501\u4e0d\u53ef\u91cd\u5165\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6b7b\u9501\nsynchronized(obj) {\n    \/\/ \u5df2\u7ecf\u83b7\u5f97 obj \u7684\u9501\n    synchronized(obj) {\n        \/\/ \u518d\u6b21\u8bf7\u6c42 obj \u7684\u9501\uff0c\u4f46\u9501\u4e0d\u53ef\u91cd\u5165 \u2192 \u6b7b\u9501\uff01\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ef\u91cd\u5165\u9501\u89e3\u51b3\u7684\u95ee\u9898\uff1a<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u907f\u514d\u6b7b\u9501<\/td><td>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u91cd\u590d\u8fdb\u5165\u4e34\u754c\u533a<\/td><\/tr><tr><td>\u5b9e\u73b0\u9012\u5f52\u8c03\u7528\u540c\u6b65<\/td><td>\u65b9\u6cd5 A \u4e2d\u8c03\u7528\u65b9\u6cd5 B\uff0c\u4e8c\u8005\u90fd\u52a0\u9501\u540c\u4e00\u4e2a\u5bf9\u8c61<\/td><\/tr><tr><td>\u7b80\u5316\u7a0b\u5e8f\u8bbe\u8ba1<\/td><td>\u65e0\u9700\u4eba\u4e3a\u8ddf\u8e2a\u662f\u5426\u5df2\u6301\u6709\u9501<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u53ef\u91cd\u5165\u9501\u5e95\u5c42\u5b9e\u73b0\u673a\u5236<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><code>synchronized<\/code>\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>JVM \u7ef4\u62a4\u4e00\u4e2a<strong>\u9501\u8ba1\u6570\u5668\uff08reentrant count\uff09<\/strong><\/li>\n\n\n\n<li>\u7b2c\u4e00\u6b21\u8fdb\u5165\uff1a\u8ba1\u6570\u5668\u4e3a 1<\/li>\n\n\n\n<li>\u518d\u6b21\u8fdb\u5165\u540c\u4e00\u9501\uff1a\u8ba1\u6570\u5668 +1<\/li>\n\n\n\n<li>\u6bcf\u6b21 <code>exit monitor<\/code>\uff1a\u8ba1\u6570\u5668 -1\uff0c\u76f4\u5230\u4e3a 0 \u624d\u771f\u6b63\u91ca\u653e\u9501<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><code>ReentrantLock<\/code>\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u4e00\u4e2a <strong>state \u53d8\u91cf\u8bb0\u5f55\u52a0\u9501\u6b21\u6570<\/strong><\/li>\n\n\n\n<li>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u91cd\u590d\u8c03\u7528 <code>lock()<\/code>\uff0cstate \u7d2f\u52a0<\/li>\n\n\n\n<li>\u6bcf\u6b21 <code>unlock()<\/code>\uff0cstate \u51cf\u4e00\uff0c\u76f4\u5230\u4e3a 0 \u624d\u91ca\u653e\u9501<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u76f8\u5173\u8d44\u6599\u63a8\u8350<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udcd8 \u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\uff1a\u7b2c 13 \u7ae0 \u53ef\u91cd\u5165\u9501<\/li>\n\n\n\n<li>\ud83d\udcd8 \u300a\u6df1\u5165\u7406\u89e3 Java \u865a\u62df\u673a\u300b\uff1a\u9501\u4f18\u5316\u4e0e\u539f\u7406<\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/github.com\/openjdk\/jdk\/blob\/master\/src\/java.base\/share\/classes\/java\/util\/concurrent\/locks\/ReentrantLock.java\">JDK ReentrantLock \u6e90\u7801<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/docs.oracle.com\/javase\/specs\/jls\/se17\/html\/jls-17.html\">JLS &#8211; Java Language Specification on synchronized<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\u53e3\u8bc0<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e00\u9501\u5728\u624b\uff0c\u518d\u8fdb\u65e0\u5fe7\uff1b\u591a\u6b21\u52a0\u9501\uff0c\u4e0d\u6b7b\u4e0d\u4f11\u3002<br>\u53ef\u91cd\u5165\u9501\u89e3\u9012\u5f52\u4e4b\u56f0\uff0c\u9632\u6b7b\u9501\u4e8e\u672a\u7136\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u201c\u53ef\u91cd\u5165\u201d \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5b83\u5173\u7cfb\u5230\u7ebf\u7a0b\u5982\u4f55\u83b7\u53d6\u9501\uff0c\u662f\u5426\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u8fdb\u5165\u540c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3918,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,86],"tags":[],"class_list":["post-3917","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3917"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3917\/revisions"}],"predecessor-version":[{"id":3919,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3917\/revisions\/3919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3918"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}