{"id":9961,"date":"2023-11-28T21:22:08","date_gmt":"2023-11-28T13:22:08","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9961"},"modified":"2023-12-07T10:33:42","modified_gmt":"2023-12-07T02:33:42","slug":"java-zhong-ru-he-xie-yi-duan-jian-dan-de-si-suo-da","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-ru-he-xie-yi-duan-jian-dan-de-si-suo-da\/","title":{"rendered":"Java\u4e2d\u5982\u4f55\u5199\u4e00\u6bb5\u7b80\u5355\u7684\u6b7b\u9501\u4ee3\u7801\uff1f"},"content":{"rendered":"<p>\u6b7b\u9501\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u79cd\u5e38\u89c1\u95ee\u9898\uff0c\u5b83\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u9501\u7684\u60c5\u51b5\u4e0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Java\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6b7b\u9501\uff1a<\/p>\n<pre><code class=\"language-java\">public class DeadlockExample {\n    private static final Object lock1 = new Object();\n    private static final Object lock2 = new Object();\n\n    public static void main(String[] args) {\n        Thread thread1 = new Thread(new Task1());\n        Thread thread2 = new Thread(new Task2());\n\n        thread1.start();\n        thread2.start();\n    }\n\n    private static class Task1 implements Runnable {\n        @Override\n        public void run() {\n            synchronized (lock1) {\n                System.out.println(&quot;Task1 acquired lock1&quot;);\n                try {\n                    \/\/ Adding some delay to increase the likelihood of deadlock\n                    Thread.sleep(100);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n\n                synchronized (lock2) {\n                    System.out.println(&quot;Task1 acquired lock2&quot;);\n                }\n            }\n        }\n    }\n\n    private static class Task2 implements Runnable {\n        @Override\n        public void run() {\n            synchronized (lock2) {\n                System.out.println(&quot;Task2 acquired lock2&quot;);\n                try {\n                    \/\/ Adding some delay to increase the likelihood of deadlock\n                    Thread.sleep(100);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n\n                synchronized (lock1) {\n                    System.out.println(&quot;Task2 acquired lock1&quot;);\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a\u7ebf\u7a0b\uff08Task1\u548cTask2\uff09\u5206\u522b\u5c1d\u8bd5\u83b7\u53d6\u4e24\u4e2a\u4e0d\u540c\u7684\u9501\uff08lock1\u548clock2\uff09\uff0c\u4f46\u662f\u5b83\u4eec\u83b7\u53d6\u9501\u7684\u987a\u5e8f\u662f\u76f8\u53cd\u7684\u3002\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u6b7b\u9501\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709lock1\u5e76\u7b49\u5f85lock2\uff0c\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709lock2\u5e76\u7b49\u5f85lock1\u3002\u5982\u679c\u4f60\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u6709\u53ef\u80fd\u4f1a\u89c2\u5bdf\u5230\u6b7b\u9501\u7684\u53d1\u751f\u3002\u6ce8\u610f\uff0c\u6b7b\u9501\u7684\u53d1\u751f\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u5b83\u53d6\u51b3\u4e8e\u7ebf\u7a0b\u7684\u8c03\u5ea6\u548c\u6267\u884c\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6b7b\u9501\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u79cd\u5e38\u89c1\u95ee\u9898\uff0c\u5b83\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u9501\u7684\u60c5\u51b5\u4e0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Java\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-9961","post","type-post","status-publish","format-standard","hentry","category-jvm"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9961","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\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=9961"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9961\/revisions"}],"predecessor-version":[{"id":16775,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9961\/revisions\/16775"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9961"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}