{"id":3292,"date":"2018-10-31T16:25:52","date_gmt":"2018-10-31T08:25:52","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3292"},"modified":"2018-10-31T16:25:52","modified_gmt":"2018-10-31T08:25:52","slug":"%e8%af%a6%e8%a7%a3sleep%e4%b8%8ewait%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3292","title":{"rendered":"\u8be6\u89e3sleep\u4e0ewait\u7684\u533a\u522b"},"content":{"rendered":"<p>\n\t\u6211\u4e4b\u524d\u5728\u516c\u4f17\u53f7\u91cc\u53d1\u4e86\u4e24\u7bc7\u5173\u4e8e\u7ebf\u7a0b\u7684\u6587\u7ae0\uff0c\u6ca1\u60f3\u5230\u5f88\u591a\u7f51\u53cb\u90fd\u60f3\u770b\uff0c\u6709\u8bc4\u8bba\u8bf4\u60f3\u770b sleep \u4e0e wait \u533a\u522b\u7684\uff0c\u4e8e\u662f\u4fbf\u6709\u4e86\u672c\u6587\u3002\n<\/p>\n<p>\n\t\u4ece\u5355\u8bcd\u7684\u610f\u601d\u4e0a\u6765\u8bf4\uff0c\u4e24\u4e2a\u90fd\u6709\u4f7f\u7ebf\u7a0b\u6682\u505c\u7684\u610f\u601d\uff0c\u4f46\u662f\u5b9e\u73b0\u539f\u7406\u4ee5\u53ca\u4f7f\u7528\u7684\u6548\u679c\u5b8c\u5168\u4e0d\u4e00\u6837\u3002\n<\/p>\n<p>\n\tsleep \u4f7f\u8ba9\u7ebf\u7a0b\u7761\u7720\u4e00\u5b9a\u7684\u65f6\u95f4\uff0c\u4e0d\u91ca\u653e\u4efb\u4f55\u9501\uff0c\u4f46\u662f\u5b83\u8fc7\u4e86\u7761\u7720\u65f6\u95f4\u540e\uff0c\u4e0d\u4e00\u5b9a\u80fd\u83b7\u5f97\u6267\u884c\u7684\u65f6\u95f4\uff01\u6700\u7ec8\u7684\u8c03\u5ea6\u662f\u7531CPU\u51b3\u5b9a\u7684\uff01Thread.sleep() \u662f\u53ef\u4ee5\u4ece\u4efb\u4f55\u4e0a\u4e0b\u6587\u8c03\u7528\u7684\u9759\u6001\u65b9\u6cd5\u3002Thread.sleep() \u6682\u505c\u5f53\u524d\u200b\u200b\u7ebf\u7a0b\uff01\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3231\" name=\"\u4ee3\u7801\u4e2d\u7684Thread.sleep(0) \u6709\u4ec0\u4e48\u610f\u4e49\uff1f\u662f\u5199\u9519\u4e86\u5417\uff1f\" target=\"_blank\" title=\"\u4ee3\u7801\u4e2d\u7684Thread.sleep(0) \u6709\u4ec0\u4e48\u610f\u4e49\uff1f\u662f\u5199\u9519\u4e86\u5417\uff1f - \u4e1a\u4f59\u8349\">\u4ee3\u7801\u4e2d\u7684Thread.sleep(0) \u6709\u4ec0\u4e48\u610f\u4e49\uff1f\u662f\u5199\u9519\u4e86\u5417\uff1f<\/a>\u300b\u3002\n<\/p>\n<p>\n\twait\uff08\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u7ebf\u7a0b\u540c\u6b65\u7684\u5b9e\u4f8b\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5728\u4efb\u4f55\u5bf9\u8c61\u4e0a\u8c03\u7528\uff0c\u56e0\u4e3a\u5b83\u5b9a\u4e49\u5728 java.lang.Object \u4e0a\uff0c \u4f46\u53ea\u80fd\u4ece\u540c\u6b65\u5757\u4e2d\u8c03\u7528\u3002\u5b83\u91ca\u653e\u5bf9\u8c61\u4e0a\u7684\u9501\uff0c\u4ee5\u4fbf\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8df3\u5165\u5e76\u83b7\u53d6\u9501\u3002wait \u4ece\u5b57\u9762\u4e0a\u6765\u7406\u89e3\u7684\u8bdd\uff0c\u5b83\u5177\u6709\u7b49\u5f85\u7684\u610f\u601d\u3002\u5b83\u7b49\u5f85&nbsp;notify \u6216 notifyAll \u5bf9\u5176\u5524\u9192\uff01\n<\/p>\n<p>\n\t\u5982\u679c\u76f4\u63a5\u8c03\u7528 wait \u4f1a\u629b\u51fa java.lang.IllegalMonitorStateException \u5f02\u5e38\uff0c\u539f\u56e0\u662f\u8fd8\u6ca1\u6709\u5f97\u5230\u5bf9\u8c61\u9501\uff0c\u6240\u4ee5\u65e0\u6cd5\u91ca\u653e\u9501\u3002\n<\/p>\n<p>\n\t\u901a\u8fc7\u4e0a\u9762\u7684\u89e3\u8bfb\uff0c\u4f60\u8ba4\u4e3a wait \u53ef\u4ee5\u4ee3\u66ff sleep \u5417\uff1f\n<\/p>\n<p>\n\t\u5f53\u7136\u662f\u66ff\u4ee3\u4e0d\u4e86\u7684\uff0c\u56e0\u4e3a\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u7279\u70b9\u5c31\u662f&nbsp;wait \u4f1a\u91ca\u653e\u9501\uff0c\u800c&nbsp;sleep \u4e0d\u4f1a\u91ca\u653e\u9501\uff01\n<\/p>\n<h2>\n\tsleep \u4e0e wait \u7684\u533a\u522b<br \/>\n<\/h2>\n<ul>\n<li>\n\t\tsleep\uff08\uff09\u7ebf\u7a0b\u63a7\u5236\u81ea\u8eab\u6d41\u7a0b\u3002\n\t<\/li>\n<li>\n\t\twait\uff08\uff09\u7528\u6765\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff0c\u4f7f\u62e5\u6709\u8be5\u5bf9\u8c61\u9501\u7684\u7ebf\u7a0b\u7b49\u5f85\u76f4\u5230\u6307\u5b9a\u65f6\u95f4\u6216notify\uff08\uff09\n\t<\/li>\n<li>\n\t\twait()\u4f1a\u91ca\u653e\u9501\u548c\u76d1\u89c6\u5668\uff0csleep()\u4e0d\u91ca\u653e\u4efb\u4f55\u9501\u6216\u76d1\u89c6\u5668\u7b49\n\t<\/li>\n<li>\n\t\twait()\u7528\u4e8e\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff0c\u800csleep()\u7528\u4e8e\u5728\u6267\u884c\u65f6\u5f15\u5165\u6682\u505c\n\t<\/li>\n<li>\n\t\t\u9002\u7528\u533a\u57df\uff0cwait\u53ea\u80fd\u653e\u5728\u540c\u6b65\u8bed\u53e5\u5757\u4e2d\u624d\u6709\u610f\u4e49\n\t<\/li>\n<li>\n\t\tsleep\u6765\u81eaThread\u7c7b\uff0c\u548cwait\u6765\u81eaObject\u7c7b\n\t<\/li>\n<li>\n\t\tsleep\u5fc5\u987b\u6355\u83b7\u5f02\u5e38\uff0c\u800cwait\uff0cnotify\u548cnotifyAll\u4e0d\u9700\u8981\u6355\u83b7\u5f02\u5e38\n\t<\/li>\n<li>\n\t\tsleep\u662fThread\u7684\u9759\u6001\u7c7b\u65b9\u6cd5\uff0c\u8c01\u8c03\u7528\u7684\u8c01\u53bb\u7761\u89c9\uff0c\u5373\u4f7f\u5728a\u7ebf\u7a0b\u91cc\u8c03\u7528\u4e86b\u7684sleep\u65b9\u6cd5\uff0c\u5b9e\u9645\u4e0a\u8fd8\u662fa\u53bb\u7761\u89c9\uff0c\u8981\u8ba9b\u7ebf\u7a0b\u7761\u89c9\u8981\u5728b\u7684\u4ee3\u7801\u4e2d\u8c03\u7528sleep\n\t<\/li>\n<li>\n\t\tThread.Sleep(0)\u7684\u4f5c\u7528\u662f&ldquo;\u89e6\u53d1\u64cd\u4f5c\u7cfb\u7edf\u7acb\u523b\u91cd\u65b0\u8fdb\u884c\u4e00\u6b21CPU\u7ade\u4e89&rdquo;\n\t<\/li>\n<li>\n\t\twait\u65b9\u6cd5\u4e00\u65e6\u8fc7\u4e86\u8d85\u65f6\u65f6\u95f4\uff0c\u5e76\u4e0d\u9700\u8981\u5176\u4ed6\u7ebf\u7a0b\u6267\u884cnotify\u4e5f\u80fd\u81ea\u52a8\u89e3\u9664\u963b\u585e\uff0c\u4f46\u662f\u5982\u679c\u6ca1\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u7684wait\u65b9\u6cd5\u5fc5\u987b\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u6267\u884cnotify\n\t<\/li>\n<\/ul>\n<p>\n\t\u5b83\u4eec\u90fd\u53ef\u4ee5\u88ab interrupted \u65b9\u6cd5\u4e2d\u65ad\u3002\u5728\u4f7f\u7528\u573a\u666f\u65b9\u9762\u90fd\u53ef\u4ee5\u5728\u751f\u4ea7\u8005\/\u6d88\u8d39\u8005\u6a21\u578b\u4e2d\u5f97\u4ee5\u5e94\u7528\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3231\" name=\"\u4ee3\u7801\u4e2d\u7684Thread.sleep(0) \u6709\u4ec0\u4e48\u610f\u4e49\uff1f\u662f\u5199\u9519\u4e86\u5417\uff1f\" target=\"_blank\" title=\"\u4ee3\u7801\u4e2d\u7684Thread.sleep(0) \u6709\u4ec0\u4e48\u610f\u4e49\uff1f\u662f\u5199\u9519\u4e86\u5417\uff1f - \u4e1a\u4f59\u8349\">\u4ee3\u7801\u4e2d\u7684Thread.sleep(0) \u6709\u4ec0\u4e48\u610f\u4e49\uff1f\u662f\u5199\u9519\u4e86\u5417\uff1f<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3218\" name=\"synchronized \u662f\u53ef\u91cd\u5165\u9501\u5417\uff1f\u4e3a\u4ec0\u4e48\uff1f\" target=\"_blank\" title=\"synchronized \u662f\u53ef\u91cd\u5165\u9501\u5417\uff1f\u4e3a\u4ec0\u4e48\uff1f - \u4e1a\u4f59\u8349\">synchronized \u662f\u53ef\u91cd\u5165\u9501\u5417\uff1f\u4e3a\u4ec0\u4e48\uff1f<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3087\" name=\"\u8fd9\u6837\u4f7f\u7528 ThreadLocal \u5bf9\u5417\uff1f\" target=\"_blank\" title=\"\u8fd9\u6837\u4f7f\u7528 ThreadLocal \u5bf9\u5417\uff1f - \u4e1a\u4f59\u8349\">\u8fd9\u6837\u4f7f\u7528 ThreadLocal \u5bf9\u5417\uff1f<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3292\">\u8be6\u89e3sleep\u4e0ewait\u7684\u533a\u522b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6211\u4e4b\u524d\u5728\u516c\u4f17\u53f7\u91cc\u53d1\u4e86\u4e24\u7bc7\u5173\u4e8e\u7ebf\u7a0b\u7684\u6587\u7ae0\uff0c\u6ca1\u60f3\u5230\u5f88\u591a\u7f51\u53cb\u90fd\u60f3\u770b\uff0c\u6709\u8bc4\u8bba\u8bf4\u60f3\u770b sleep \u4e0e wait \u533a\u522b\u7684\uff0c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[565,202,1098,883,1104],"class_list":["post-3292","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-","tag-locked","tag-1098","tag-883","tag-1104"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3292"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3292\/revisions"}],"predecessor-version":[{"id":3293,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3292\/revisions\/3293"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}