{"id":67065,"date":"2025-05-03T12:46:01","date_gmt":"2025-05-03T04:46:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/67065\/"},"modified":"2025-05-03T12:46:01","modified_gmt":"2025-05-03T04:46:01","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-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/67065\/","title":{"rendered":"\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1"},"content":{"rendered":"<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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u6b65\u5757\uff1a<\/p>\n<pre>synchronized (this) {\n    \/\/ c\u00f3digo sincronizado\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6c9f\u901a\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u7ebf\u7a0b\u4e4b\u95f4\u4f7f\u7528wt()\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173206431978074.jpg\" class=\"aligncenter\" title=\"\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1\u63d2\u56fe\" alt=\"\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1\u63d2\u56fe\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9644\u52a0\u5185\u5bb9\uff1a \u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u4e0e\u901a\u4fe1\u95ee\u9898\uff1a \u7ebf\u7a0b\u5728\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u4e92\u76f8\u5e72\u6270\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u540c\u6b65\u65b9\u6cd5 synchronized void synchronizedmethod() { \/\/ c\u00f3digo sincronizado } \u767b\u5f55\u540e\u590d\u5236 \u540c\u6b65\u5757\uff1a synchronized (this) { \/\/ c\u00f3digo sincronizado } \u767b\u5f55\u540e\u590d\u5236 \u6c9f\u901a\u793a\u4f8b\uff1a \u7ebf\u7a0b\u4e4b\u95f4\u4f7f\u7528wt()\u3001notify()\u548cnotifyall()\u8fdb\u884c\u901a\u4fe1\uff1a class SharedResource { private boolean flag = false; synchronized void produce() { while (flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(&#8220;Producing&#8230;&#8221;); flag = true; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-67065","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=67065"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67065\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=67065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=67065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=67065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}