{"id":203841,"date":"2025-05-22T10:57:02","date_gmt":"2025-05-22T02:57:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203841\/"},"modified":"2025-05-22T10:57:02","modified_gmt":"2025-05-22T02:57:02","slug":"%e7%ba%bf%e7%a8%8b%e9%97%b4%e7%9a%84%e5%90%8c%e6%ad%a5%e5%92%8c%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203841\/","title":{"rendered":"\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>\u9644\u52a0\u5185\u5bb9\uff1a<\/p>\n<p><strong>\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u4e0e\u901a\u4fe1<\/strong><br \/><strong>\u95ee\u9898\uff1a<\/strong> \u7ebf\u7a0b\u5728\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u4e92\u76f8\u5e72\u6270\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><br \/> \u540c\u6b65\u65b9\u6cd5<\/p>\n<pre>synchronized void synchronizedmethod() {\n    \/\/ c\u00f3digo sincronizado\n}\n\n<\/pre>\n<p>\u540c\u6b65\u5757\uff1a<\/p>\n<pre>synchronized (this) {\n    \/\/ c\u00f3digo sincronizado\n}\n\n<\/pre>\n<p><strong>\u6c9f\u901a\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u7ebf\u7a0b\u4e4b\u95f4\u4f7f\u7528wait()\u3001notify()\u548cnotifyall()\u8fdb\u884c\u901a\u4fe1\uff1a<\/p>\n<pre>class SharedResource {\n    private boolean flag = false;\n\n    synchronized void produce() {\n        while (flag) {\n            try {\n                wait();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n        System.out.println(\"Producing...\");\n        flag = true;\n        notify();\n    }\n\n    synchronized void consume() {\n        while (!flag) {\n            try {\n                wait();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n        System.out.println(\"Consuming...\");\n        flag = false;\n        notify();\n    }\n}\n\npublic class ThreadCommunication {\n    public static void main(String[] args) {\n        SharedResource resource = new SharedResource();\n\n        Thread producer = new Thread(resource::produce);\n        Thread consumer = new Thread(resource::consume);\n\n        producer.start();\n        consumer.start();\n    }\n}\n\n<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<ul>\n<li>java \u4e2d\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\u5141\u8bb8\u60a8\u521b\u5efa\u66f4\u9ad8\u6548\u200b\u200b\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c24\u5176\u662f\u5728\u591a\u6838\u7cfb\u7edf\u4e0a\u3002<\/li>\n<li>\u4f7f\u7528\u540c\u6b65\u6b63\u786e\u7ba1\u7406\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<li>thread \u7c7b\u65b9\u6cd5\u548c runnable \u63a5\u53e3\u662f\u5904\u7406\u591a\u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17327059806746febc489d9.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1 \u6536\u85cf \u4e00\u5206\u8015&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203841","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203841"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203841\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}