{"id":37521,"date":"2023-12-11T14:41:46","date_gmt":"2023-12-11T06:41:46","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37521"},"modified":"2023-12-11T14:41:46","modified_gmt":"2023-12-11T06:41:46","slug":"%e4%bb%80%e4%b9%88%e6%98%af%e7%ba%bf%e7%a8%8b%e6%ad%bb%e9%94%81%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%ad%bb%e9%94%81","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e4%bb%80%e4%b9%88%e6%98%af%e7%ba%bf%e7%a8%8b%e6%ad%bb%e9%94%81%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%ad%bb%e9%94%81\/","title":{"rendered":"\u4ec0\u4e48\u662f\u7ebf\u7a0b\u6b7b\u9501?\u5982\u4f55\u907f\u514d\u6b7b\u9501?"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>&lt;h3&gt;\u8ba4\u8bc6\u7ebf\u7a0b\u6b7b\u9501&lt;\/h3&gt;<\/p>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u88ab\u963b\u585e\uff0c\u5b83\u4eec\u4e2d\u7684\u4e00\u4e2a\u6216\u8005\u5168\u90e8\u90fd\u5728\u7b49\u5f85\u67d0\u4e2a\u8d44\u6e90\u88ab\u91ca\u653e\u3002\u7531\u4e8e\u7ebf\u7a0b\u88ab\u65e0\u9650\u671f\u5730\u963b\u585e\uff0c\u56e0\u6b64\u7a0b\u5e8f\u4e0d\u53ef\u80fd\u6b63\u5e38\u7ec8\u6b62\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u7ebf\u7a0b A \u6301\u6709\u8d44\u6e90 2\uff0c\u7ebf\u7a0b B \u6301\u6709\u8d44\u6e90 1\uff0c\u4ed6\u4eec\u540c\u65f6\u90fd\u60f3\u7533\u8bf7\u5bf9\u65b9\u7684\u8d44\u6e90\uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u4e92\u76f8\u7b49\u5f85\u800c\u8fdb\u5165\u6b7b\u9501\u72b6\u6001\u3002<\/p>\n<p>&lt;img src=&quot;&quot;https:\/\/my-blog-to-use.oss-cn-beijing.aliyuncs.com\/2019-4\/2019-4%E6%AD%BB%E9%94%811.png&quot;&quot; referrerpolicy=&quot;&quot;no-referrer&quot;&quot; alt=&quot;&quot;\u7ebf\u7a0b\u6b7b\u9501\u793a\u610f\u56fe &quot;&quot; style=&quot;&quot;width: 100%;&quot;&quot;&gt;<\/p>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u7ebf\u7a0b\u6b7b\u9501,\u4ee3\u7801\u6a21\u62df\u4e86\u4e0a\u56fe\u7684\u6b7b\u9501\u7684\u60c5\u51b5 (\u4ee3\u7801\u6765\u6e90\u4e8e\u300a\u5e76\u53d1\u7f16\u7a0b\u4e4b\u7f8e\u300b)\uff1a&lt;\/p&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public class DeadLockDemo {    private static Object resource1 = new Object();\/\/\u8d44\u6e90 1    private static Object resource2 = new Object();\/\/\u8d44\u6e90 2    public static void main(String[] args) {        new Thread(() -&gt; {            synchronized (resource1) {                System.out.println(Thread.currentThread() + &quot;&quot;get resource1&quot;&quot;);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread() + &quot;&quot;waiting get resource2&quot;&quot;);                synchronized (resource2) {                    System.out.println(Thread.currentThread() + &quot;&quot;get resource2&quot;&quot;);                }            }        }, &quot;&quot;\u7ebf\u7a0b 1&quot;&quot;).start();        new Thread(() -&gt; {            synchronized (resource2) {                System.out.println(Thread.currentThread() + &quot;&quot;get resource2&quot;&quot;);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread() + &quot;&quot;waiting get resource1&quot;&quot;);                synchronized (resource1) {                    System.out.println(Thread.currentThread() + &quot;&quot;get resource1&quot;&quot;);                }            }        }, &quot;&quot;\u7ebf\u7a0b 2&quot;&quot;).start();    }}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>Output<\/p>\n<p>&lt;pre&gt;&lt;code&gt;Thread[\u7ebf\u7a0b 1,5,main]get resource1Thread[\u7ebf\u7a0b 2,5,main]get resource2Thread[\u7ebf\u7a0b 1,5,main]waiting get resource2Thread[\u7ebf\u7a0b 2,5,main]waiting get resource1&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u7ebf\u7a0b A \u901a\u8fc7 synchronized (resource1) \u83b7\u5f97 resource1 \u7684\u76d1\u89c6\u5668\u9501\uff0c\u7136\u540e\u901a\u8fc7&lt;code&gt;Thread.sleep(1000)&lt;span style=&quot;&quot;font-family:&quot;Open Sans&quot;, &quot;Clear Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif&quot;&quot;&gt;;&lt;\/span&gt;&lt;\/code&gt;\u8ba9\u7ebf\u7a0b A \u4f11\u7720 1s \u4e3a\u7684\u662f\u8ba9\u7ebf\u7a0b B \u5f97\u5230\u6267\u884c\u7136\u540e\u83b7\u53d6\u5230 resource2 \u7684\u76d1\u89c6\u5668\u9501\u3002\u7ebf\u7a0b A \u548c\u7ebf\u7a0b B \u4f11\u7720\u7ed3\u675f\u4e86\u90fd\u5f00\u59cb\u4f01\u56fe\u8bf7\u6c42\u83b7\u53d6\u5bf9\u65b9\u7684\u8d44\u6e90\uff0c\u7136\u540e\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u9677\u5165\u4e92\u76f8\u7b49\u5f85\u7684\u72b6\u6001\uff0c\u8fd9\u4e5f\u5c31\u4ea7\u751f\u4e86\u6b7b\u9501\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u7b26\u5408\u4ea7\u751f\u6b7b\u9501\u7684\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6\u3002<\/p>\n<p>\u5b66\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7684\u670b\u53cb\u90fd\u77e5\u9053\u4ea7\u751f\u6b7b\u9501\u5fc5\u987b\u5177\u5907\u4ee5\u4e0b\u56db\u4e2a\u6761\u4ef6\uff1a<\/p>\n<p>&lt;ol&gt;&lt;li&gt;\u4e92\u65a5\u6761\u4ef6\uff1a\u8be5\u8d44\u6e90\u4efb\u610f\u4e00\u4e2a\u65f6\u523b\u53ea\u7531\u4e00\u4e2a\u7ebf\u7a0b\u5360\u7528\u3002&lt;\/li&gt;&lt;li&gt;\u8bf7\u6c42\u4e0e\u4fdd\u6301\u6761\u4ef6\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u56e0\u8bf7\u6c42\u8d44\u6e90\u800c\u963b\u585e\u65f6\uff0c\u5bf9\u5df2\u83b7\u5f97\u7684\u8d44\u6e90\u4fdd\u6301\u4e0d\u653e\u3002&lt;\/li&gt;&lt;li&gt;\u4e0d\u5265\u593a\u6761\u4ef6:\u7ebf\u7a0b\u5df2\u83b7\u5f97\u7684\u8d44\u6e90\u5728\u672b\u4f7f\u7528\u5b8c\u4e4b\u524d\u4e0d\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5f3a\u884c\u5265\u593a\uff0c\u53ea\u6709\u81ea\u5df1\u4f7f\u7528\u5b8c\u6bd5\u540e\u624d\u91ca\u653e\u8d44\u6e90\u3002&lt;\/li&gt;&lt;li&gt;\u5faa\u73af\u7b49\u5f85\u6761\u4ef6:\u82e5\u5e72\u8fdb\u7a0b\u4e4b\u95f4\u5f62\u6210\u4e00\u79cd\u5934\u5c3e\u76f8\u63a5\u7684\u5faa\u73af\u7b49\u5f85\u8d44\u6e90\u5173\u7cfb\u3002&lt;\/li&gt;&lt;\/ol&gt;<\/p>\n<p>&lt;h3&gt;\u5982\u4f55\u9884\u9632\u7ebf\u7a0b\u6b7b\u9501?&lt;\/h3&gt;<\/p>\n<p>\u6211\u4eec\u53ea\u8981\u7834\u574f\u4ea7\u751f\u6b7b\u9501\u7684\u56db\u4e2a\u6761\u4ef6\u4e2d\u7684\u5176\u4e2d\u4e00\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>&lt;strong&gt;\u7834\u574f\u4e92\u65a5\u6761\u4ef6&lt;\/strong&gt;<\/p>\n<p>\u8fd9\u4e2a\u6761\u4ef6\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u7834\u574f\uff0c\u56e0\u4e3a\u6211\u4eec\u7528\u9501\u672c\u6765\u5c31\u662f\u60f3\u8ba9\u4ed6\u4eec\u4e92\u65a5\u7684\uff08\u4e34\u754c\u8d44\u6e90\u9700\u8981\u4e92\u65a5\u8bbf\u95ee\uff09\u3002<\/p>\n<p>&lt;strong&gt;\u7834\u574f\u8bf7\u6c42\u4e0e\u4fdd\u6301\u6761\u4ef6&lt;\/strong&gt;<\/p>\n<p>\u4e00\u6b21\u6027\u7533\u8bf7\u6240\u6709\u7684\u8d44\u6e90\u3002<\/p>\n<p>&lt;strong&gt;\u7834\u574f\u4e0d\u5265\u593a\u6761\u4ef6&lt;\/strong&gt;<\/p>\n<p>\u5360\u7528\u90e8\u5206\u8d44\u6e90\u7684\u7ebf\u7a0b\u8fdb\u4e00\u6b65\u7533\u8bf7\u5176\u4ed6\u8d44\u6e90\u65f6\uff0c\u5982\u679c\u7533\u8bf7\u4e0d\u5230\uff0c\u53ef\u4ee5\u4e3b\u52a8\u91ca\u653e\u5b83\u5360\u6709\u7684\u8d44\u6e90\u3002<\/p>\n<p>&lt;strong&gt;\u7834\u574f\u5faa\u73af\u7b49\u5f85\u6761\u4ef6&lt;\/strong&gt;<\/p>\n<p>\u9760\u6309\u5e8f\u7533\u8bf7\u8d44\u6e90\u6765\u9884\u9632\u3002\u6309\u67d0\u4e00\u987a\u5e8f\u7533\u8bf7\u8d44\u6e90\uff0c\u91ca\u653e\u8d44\u6e90\u5219\u53cd\u5e8f\u91ca\u653e\u3002\u7834\u574f\u5faa\u73af\u7b49\u5f85\u6761\u4ef6\u3002<\/p>\n<p>\u6211\u4eec\u5bf9\u7ebf\u7a0b 2 \u7684\u4ee3\u7801\u4fee\u6539\u6210\u4e0b\u9762\u8fd9\u6837\u5c31\u4e0d\u4f1a\u4ea7\u751f\u6b7b\u9501\u4e86\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;        new Thread(() -&gt; {            synchronized (resource1) {                System.out.println(Thread.currentThread() + &quot;&quot;get resource1&quot;&quot;);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread() + &quot;&quot;waiting get resource2&quot;&quot;);                synchronized (resource2) {                    System.out.println(Thread.currentThread() + &quot;&quot;get resource2&quot;&quot;);                }            }        }, &quot;&quot;\u7ebf\u7a0b 2&quot;&quot;).start();&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>Output<\/p>\n<p>&lt;pre&gt;&lt;code&gt;Thread[\u7ebf\u7a0b 1,5,main]get resource1Thread[\u7ebf\u7a0b 1,5,main]waiting get resource2Thread[\u7ebf\u7a0b 1,5,main]get resource2Thread[\u7ebf\u7a0b 2,5,main]get resource1Thread[\u7ebf\u7a0b 2,5,main]waiting get resource2Thread[\u7ebf\u7a0b 2,5,main]get resource2Process finished with exit code 0&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u6211\u4eec\u5206\u6790\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\u4e3a\u4ec0\u4e48\u907f\u514d\u4e86\u6b7b\u9501\u7684\u53d1\u751f?<\/p>\n<p>\u7ebf\u7a0b 1 \u9996\u5148\u83b7\u5f97\u5230 resource1 \u7684\u76d1\u89c6\u5668\u9501,\u8fd9\u65f6\u5019\u7ebf\u7a0b 2 \u5c31\u83b7\u53d6\u4e0d\u5230\u4e86\u3002\u7136\u540e\u7ebf\u7a0b 1 \u518d\u53bb\u83b7\u53d6 resource2 \u7684\u76d1\u89c6\u5668\u9501\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u3002\u7136\u540e\u7ebf\u7a0b 1 \u91ca\u653e\u4e86\u5bf9 resource1\u3001resource2 \u7684\u76d1\u89c6\u5668\u9501\u7684\u5360\u7528\uff0c\u7ebf\u7a0b 2 \u83b7\u53d6\u5230\u5c31\u53ef\u4ee5\u6267\u884c\u4e86\u3002\u8fd9\u6837\u5c31\u7834\u574f\u4e86\u7834\u574f\u5faa\u73af\u7b49\u5f85\u6761\u4ef6\uff0c\u56e0\u6b64\u907f\u514d\u4e86\u6b7b\u9501\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; &lt;h3&gt;\u8ba4\u8bc6\u7ebf\u7a0b\u6b7b\u9501&lt;\/h3&gt; \u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u88ab\u963b\u585e\uff0c\u5b83 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37521","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37521"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37521\/revisions"}],"predecessor-version":[{"id":37522,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37521\/revisions\/37522"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37521"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}