{"id":35079,"date":"2024-11-26T09:35:27","date_gmt":"2024-11-26T01:35:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35079\/"},"modified":"2024-11-26T09:35:27","modified_gmt":"2024-11-26T01:35:27","slug":"java-%e4%b8%ad-wait-%e5%92%8c-sleep-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35079\/","title":{"rendered":"Java \u4e2d wait() \u548c sleep() \u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173060299627127.jpg\" class=\"aligncenter\" title=\"Java \u4e2d wait() \u548c sleep() \u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"Java \u4e2d wait() \u548c sleep() \u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d wt() \u548c sleep() \u7684\u5173\u952e\u5dee\u5f02<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c<strong>wait()\u548c sleep()<\/strong> \u65b9\u6cd5\u90fd\u7528\u4e8e\u6682\u505c\u6267\u884c\u4e2d\u7684\u7ebf\u7a0b\u3002\u4f46\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5728\u529f\u80fd\u4e0a\u6709\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u83b7\u53d6\u9501<\/strong><\/p>\n<ul>\n<li> <strong>wait()\uff1a<\/strong> \u53ea\u80fd\u5728<strong>synchronized<\/strong>\u5757\u5185\u8c03\u7528\u3002\u8fd9\u8868\u793a\u5b83\u4f1a\u81ea\u52a8\u83b7\u53d6\u5bf9\u8c61\u9501\u3002<\/li>\n<li> <strong>sleep()\uff1a<\/strong> \u4e0d\u9700\u8981\u9501\u3002\u5b83\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u4efb\u4f55\u90e8\u5206\u8c03\u7528\uff0c\u5305\u62ec\u975e\u540c\u6b65\u5757\u3002<\/li>\n<\/ul>\n<p><strong>\u91ca\u653e\u9501<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li> <strong>wait()\uff1a<\/strong> \u5728\u6267\u884c\u65f6\u4f1a\u91ca\u653e\u9501\u3002\u4e00\u65e6\u7ebf\u7a0b\u88ab\u5524\u9192\u6216\u4e2d\u65ad\uff0c\u5b83\u5c06\u91cd\u65b0\u83b7\u53d6\u9501\u3002<\/li>\n<li> <strong>sleep()\uff1a<\/strong> \u5728\u6267\u884c\u65f6<strong>\u4e0d\u4f1a<\/strong>\u91ca\u653e\u9501\u3002\u5373\u4f7f\u7ebf\u7a0b\u88ab\u4e2d\u65ad\uff0c\u5b83\u4ecd\u4f1a\u6301\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbe\u7f6e\u65f6\u95f4<\/strong><\/p>\n<ul>\n<li> <strong>sleep()\uff1a<\/strong> \u5fc5\u987b\u6307\u5b9a\u4e00\u4e2a\u65f6\u95f4\u53c2\u6570\u6765\u786e\u5b9a\u7ebf\u7a0b\u5e94\u8be5\u7761\u7720\u7684\u6301\u7eed\u65f6\u95f4\u3002<\/li>\n<li> <strong>wait()\uff1a<\/strong> \u6ca1\u6709\u53c2\u6570\uff0c\u56e0\u6b64\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u7b49\u5f85\uff0c\u76f4\u5230\u88ab\u5524\u9192\u6216\u4e2d\u65ad\u3002<\/li>\n<\/ul>\n<p><strong>\u6240\u5c5e\u7c7b<\/strong><\/p>\n<ul>\n<li> <strong>wait()\uff1a<\/strong> \u5b9a\u4e49\u5728 <strong>Object<\/strong> \u7c7b\u4e2d\uff0c\u662f <strong>Object<\/strong> \u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/li>\n<li> <strong>sleep()\uff1a<\/strong> \u5b9a\u4e49\u5728 <strong>Thread<\/strong> \u7c7b\u4e2d\uff0c\u662f <strong>Thread<\/strong> \u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u5dee\u5f02<\/strong><\/p>\n<ul>\n<li> <strong>wait()\uff1a<\/strong> \u4e0d\u4f1a\u54cd\u5e94 <strong>Thread.interrupt()<\/strong>\uff0c\u5fc5\u987b\u4f7f\u7528 <strong>notify()<\/strong> \u6216 <strong>notifyAll()<\/strong> \u65b9\u6cd5\u6765\u5524\u9192\u7ebf\u7a0b\u3002<\/li>\n<li> <strong>sleep()\uff1a<\/strong> \u4f1a\u54cd\u5e94 <strong>Thread.interrupt()<\/strong>\uff0c\u5e76\u629b\u51fa <strong>InterruptedException<\/strong>\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJava \u4e2d wt() \u548c sleep() \u6709\u4ec0\u4e48\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u4e2d wt() \u548c sleep() \u7684\u5173\u952e\u5dee\u5f02 \u5728 Java \u4e2d\uff0cwait()\u548c sleep() \u65b9\u6cd5\u90fd\u7528\u4e8e\u6682\u505c\u6267\u884c\u4e2d\u7684\u7ebf\u7a0b\u3002\u4f46\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5728\u529f\u80fd\u4e0a\u6709\u91cd\u8981\u7684\u3002 \u83b7\u53d6\u9501 wait()\uff1a \u53ea\u80fd\u5728synchronized\u5757\u5185\u8c03\u7528\u3002\u8fd9\u8868\u793a\u5b83\u4f1a\u81ea\u52a8\u83b7\u53d6\u5bf9\u8c61\u9501\u3002 sleep()\uff1a \u4e0d\u9700\u8981\u9501\u3002\u5b83\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u4efb\u4f55\u90e8\u5206\u8c03\u7528\uff0c\u5305\u62ec\u975e\u540c\u6b65\u5757\u3002 \u91ca\u653e\u9501 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b wait()\uff1a \u5728\u6267\u884c\u65f6\u4f1a\u91ca\u653e\u9501\u3002\u4e00\u65e6\u7ebf\u7a0b\u88ab\u5524\u9192\u6216\u4e2d\u65ad\uff0c\u5b83\u5c06\u91cd\u65b0\u83b7\u53d6\u9501\u3002 sleep()\uff1a \u5728\u6267\u884c\u65f6\u4e0d\u4f1a\u91ca\u653e\u9501\u3002\u5373\u4f7f\u7ebf\u7a0b\u88ab\u4e2d\u65ad\uff0c\u5b83\u4ecd\u4f1a\u6301\u3002 \u8bbe\u7f6e\u65f6\u95f4 sleep()\uff1a \u5fc5\u987b\u6307\u5b9a\u4e00\u4e2a\u65f6\u95f4\u53c2\u6570\u6765\u786e\u5b9a\u7ebf\u7a0b\u5e94\u8be5\u7761\u7720\u7684\u6301\u7eed\u65f6\u95f4\u3002 wait()\uff1a \u6ca1\u6709\u53c2\u6570\uff0c\u56e0\u6b64\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u7b49\u5f85\uff0c\u76f4\u5230\u88ab\u5524\u9192\u6216\u4e2d\u65ad\u3002 \u6240\u5c5e\u7c7b wait()\uff1a \u5b9a\u4e49\u5728 Object \u7c7b\u4e2d\uff0c\u662f Object \u7684\u4e00\u4e2a\u65b9\u6cd5\u3002 sleep()\uff1a \u5b9a\u4e49\u5728 Thread \u7c7b\u4e2d\uff0c\u662f Thread \u7684\u4e00\u4e2a\u65b9\u6cd5\u3002 \u5176\u4ed6\u5dee\u5f02 wait()\uff1a \u4e0d\u4f1a\u54cd\u5e94 Thread.interrupt()\uff0c\u5fc5\u987b\u4f7f\u7528 notify() \u6216 notifyAll() \u65b9\u6cd5\u6765\u5524\u9192\u7ebf\u7a0b\u3002 sleep()\uff1a \u4f1a\u54cd\u5e94 Thread.interrupt()\uff0c\u5e76\u629b\u51fa InterruptedException\u3002 \u4ee5\u4e0a\u5c31\u662fJava \u4e2d wt() [&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-35079","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35079","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=35079"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35079\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}