{"id":64712,"date":"2025-05-03T13:32:25","date_gmt":"2025-05-03T05:32:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64712\/"},"modified":"2025-05-03T13:32:25","modified_gmt":"2025-05-03T05:32:25","slug":"%e7%94%9f%e4%ba%a7%e8%80%85-%e6%b6%88%e8%b4%b9%e8%80%85%e9%97%ae%e9%a2%98%e4%b8%ad%e4%b8%ba%e4%bb%80%e4%b9%88%e5%8f%aa%e6%9c%89%e4%b8%80%e4%b8%aa%e6%b6%88%e8%b4%b9%e8%80%85%e5%8f%82%e4%b8%8e-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64712\/","title":{"rendered":"\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u4e2d\u4e3a\u4ec0\u4e48\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173068747258062.jpg\" class=\"aligncenter\" title=\"\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u4e2d\u4e3a\u4ec0\u4e48\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff1f\u63d2\u56fe\" alt=\"\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u4e2d\u4e3a\u4ec0\u4e48\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>\u4e3a\u4ec0\u4e48\u5728\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u4e2d\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff1f<\/h2>\n<p>\u5728\u60a8\u63d0\u4f9b\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u793a\u4f8b\u4e2d\uff0c\u51fa\u73b0\u4e86\u4ee5\u4e0b\u95ee\u9898\uff1a\u53ea\u6709\u5355\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff0c\u800c\u6709\u591a\u4e2a\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u95ee\u9898\u6e90\u81ea wt() \u548c notifyall() \u65b9\u6cd5\u7684\u884c\u4e3a\u3002<\/p>\n<ul>\n<li>list.wait() \u53ea\u80fd\u7531\u6301\u6709 list \u9501\u7684\u7ebf\u7a0b\u8c03\u7528\u3002<\/li>\n<li>list.notifyall() \u53ea\u4f1a\u5524\u9192\u6240\u6709\u56e0\u8c03\u7528 list \u9501\u7684 wait() \u800c\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u7684\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u60a8\u7684\u7a0b\u5e8f\u6267\u884c\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ade\u4e89\u83b7\u53d6 list \u9501\u3002\u5047\u8bbe\u751f\u4ea7\u8005\u9996\u5148\u83b7\u5f97\u9501\u3002<\/li>\n<li>\u751f\u4ea7\u8005\u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u8c03\u7528 notifyall() \u5524\u9192\u6240\u6709\u7b49\u5f85\u7ebf\u7a0b\u3002<\/li>\n<li>\u6d88\u8d39\u8005 1 \u548c \u6d88\u8d39\u8005 2 \u7ade\u4e89\u83b7\u53d6\u9501\u3002\u5047\u8bbe\u6d88\u8d39\u8005 1 \u83b7\u53d6\u9501\u3002<\/li>\n<li>\u6d88\u8d39\u8005 1 \u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u8c03\u7528 notifyall()\uff0c\u4f46\u53ea\u5524\u9192\u751f\u4ea7\u8005\uff0c\u56e0\u4e3a\u53ea\u6709\u751f\u4ea7\u8005\u5728\u8c03\u7528 list.wait() \u540e\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u3002<\/li>\n<li>\u751f\u4ea7\u8005\u88ab\u5524\u9192\uff0c\u5904\u7406\u5b8c\u6570\u636e\u540e\u518d\u6b21\u8c03\u7528 notifyall()\uff0c\u4f46\u4ecd\u7136\u53ea\u5524\u9192\u6d88\u8d39\u8005 1\u3002<\/li>\n<\/ol>\n<p>\u5982\u6b64\u5faa\u73af\uff0c\u5bfc\u81f4\u53ea\u6709\u6d88\u8d39\u8005 1 \u80fd\u591f\u53c2\u4e0e\u6d88\u8d39\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u6807\u5fd7\u6765\u6307\u793a\u4f55\u65f6\u5524\u9192\u751f\u4ea7\u8005\uff0c\u4ee5\u53ca\u4f55\u65f6\u5524\u9192\u6d88\u8d39\u8005\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>private boolean isconsumerwaiting = false;\n\nsynchronized (list) {\n    while (true) {\n        \/\/ \u5982\u679c\u6ca1\u6709\u6d88\u8d39\u8005\u7b49\u5f85\u5e76\u4e14 list \u5927\u5c0f\u4e3a 0\uff0c\u5219\u7b49\u5f85\n        if (!isconsumerwaiting &amp;&amp; list.size() == 0) {\n            try {\n                list.wait();\n            } catch (interruptedexception e) {\n                e.printstacktrace();\n            }\n        }\n\n        \/\/ \u5426\u5219\uff0c\u751f\u4ea7\u6570\u636e\n        object object = new object();\n        list.add(object);\n        system.out.println(thread.currentthread().getname() + \"\u751f\u4ea7 list\" + object);\n\n        \/\/ \u8bbe\u7f6e\u6d88\u8d39\u8005\u7b49\u5f85\u6807\u5fd7\u4e3a true\n        isconsumerwaiting = true;\n\n        \/\/ \u5524\u9192\u6240\u6709\u6d88\u8d39\u8005\n        list.notifyall();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u6d88\u8d39\u7ebf\u7a0b\u4e2d\uff1a<\/p>\n<pre>synchronized (list) {\n    while (true) {\n        \/\/ \u5982\u679c\u6ca1\u6709\u53ef\u6d88\u8d39\u7684\u6570\u636e\u4e14\u6d88\u8d39\u8005\u7b49\u5f85\uff0c\u5219\u7b49\u5f85\n        if (list.size() == 0 &amp;&amp; isConsumerWaiting) {\n            try {\n                list.wait();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n\n        \/\/ \u5426\u5219\uff0c\u6d88\u8d39\u6570\u636e\n        Object object = list.remove(0);\n        System.out.println(Thread.currentThread().getName() + \"\u6d88\u8d39 list\" + object);\n\n        \/\/ \u91cd\u7f6e\u6d88\u8d39\u8005\u7b49\u5f85\u6807\u5fd7\u4e3a false\n        isConsumerWaiting = false;\n\n        \/\/ \u5524\u9192\u6240\u6709\u751f\u4ea7\u8005\n        list.notifyAll();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u4e2d\u4e3a\u4ec0\u4e48\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff1f\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>\u4e3a\u4ec0\u4e48\u5728\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u4e2d\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff1f \u5728\u60a8\u63d0\u4f9b\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u793a\u4f8b\u4e2d\uff0c\u51fa\u73b0\u4e86\u4ee5\u4e0b\u95ee\u9898\uff1a\u53ea\u6709\u5355\u4e2a\u6d88\u8d39\u8005\u53c2\u4e0e\u6d88\u8d39\uff0c\u800c\u6709\u591a\u4e2a\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002 \u539f\u56e0\uff1a \u95ee\u9898\u6e90\u81ea wt() \u548c notifyall() \u65b9\u6cd5\u7684\u884c\u4e3a\u3002 list.wait() \u53ea\u80fd\u7531\u6301\u6709 list \u9501\u7684\u7ebf\u7a0b\u8c03\u7528\u3002 list.notifyall() \u53ea\u4f1a\u5524\u9192\u6240\u6709\u56e0\u8c03\u7528 list \u9501\u7684 wait() \u800c\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u7684\u7ebf\u7a0b\u3002 \u56e0\u6b64\uff0c\u60a8\u7684\u7a0b\u5e8f\u6267\u884c\u5982\u4e0b\uff1a \u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ade\u4e89\u83b7\u53d6 list \u9501\u3002\u5047\u8bbe\u751f\u4ea7\u8005\u9996\u5148\u83b7\u5f97\u9501\u3002 \u751f\u4ea7\u8005\u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u8c03\u7528 notifyall() \u5524\u9192\u6240\u6709\u7b49\u5f85\u7ebf\u7a0b\u3002 \u6d88\u8d39\u8005 1 \u548c \u6d88\u8d39\u8005 2 \u7ade\u4e89\u83b7\u53d6\u9501\u3002\u5047\u8bbe\u6d88\u8d39\u8005 1 \u83b7\u53d6\u9501\u3002 \u6d88\u8d39\u8005 1 \u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u8c03\u7528 notifyall()\uff0c\u4f46\u53ea\u5524\u9192\u751f\u4ea7\u8005\uff0c\u56e0\u4e3a\u53ea\u6709\u751f\u4ea7\u8005\u5728\u8c03\u7528 list.wait() \u540e\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u3002 \u751f\u4ea7\u8005\u88ab\u5524\u9192\uff0c\u5904\u7406\u5b8c\u6570\u636e\u540e\u518d\u6b21\u8c03\u7528 notifyall()\uff0c\u4f46\u4ecd\u7136\u53ea\u5524\u9192\u6d88\u8d39\u8005 1\u3002 \u5982\u6b64\u5faa\u73af\uff0c\u5bfc\u81f4\u53ea\u6709\u6d88\u8d39\u8005 1 \u80fd\u591f\u53c2\u4e0e\u6d88\u8d39\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a \u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u6807\u5fd7\u6765\u6307\u793a\u4f55\u65f6\u5524\u9192\u751f\u4ea7\u8005\uff0c\u4ee5\u53ca\u4f55\u65f6\u5524\u9192\u6d88\u8d39\u8005\u3002 \u4f8b\u5982\uff1a private boolean isconsumerwaiting = false; synchronized (list) [&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-64712","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64712","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=64712"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64712\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}