{"id":121129,"date":"2018-09-30T08:26:35","date_gmt":"2018-09-30T00:26:35","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=121129"},"modified":"2018-09-25T11:27:14","modified_gmt":"2018-09-25T03:27:14","slug":"sleep-wait","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sleep-wait.html","title":{"rendered":"\u6d45\u8c08sleep\u4e0ewait\u7684\u533a\u522b"},"content":{"rendered":"
\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u6765\u81ea\u4e0d\u540c\u7684\u7c7b\u5206\u522b\u662f\uff0csleep\u6765\u81eaThread\u7c7b\uff0c\u548cwait\u6765\u81eaObject\u7c7b\u3002<\/p>\n
sleep\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\u3002<\/p>\n
\u6700\u4e3b\u8981\u662fsleep\u65b9\u6cd5\u6ca1\u6709\u91ca\u653e\u9501\uff0c\u800cwait\u65b9\u6cd5\u91ca\u653e\u4e86\u9501\uff0c\u4f7f\u5f97\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528\u540c\u6b65\u63a7\u5236\u5757\u6216\u8005\u65b9\u6cd5\u3002<\/p>\n
sleep\u4e0d\u51fa\u8ba9\u7cfb\u7edf\u8d44\u6e90\uff1bwait\u662f\u8fdb\u5165\u7ebf\u7a0b\u7b49\u5f85\u6c60\u7b49\u5f85\uff0c\u51fa\u8ba9\u7cfb\u7edf\u8d44\u6e90\uff0c\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u5360\u7528CPU\u3002\u4e00\u822cwait\u4e0d\u4f1a\u52a0\u65f6\u95f4\u9650\u5236\uff0c\u56e0\u4e3a\u5982\u679cwait\u7ebf\u7a0b\u7684\u8fd0\u884c\u8d44\u6e90\u4e0d\u591f\uff0c\u518d\u51fa\u6765\u4e5f\u6ca1\u7528\uff0c\u8981\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528notify\/notifyAll\u5524\u9192\u7b49\u5f85\u6c60\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\uff0c\u624d\u4f1a\u8fdb\u5165\u5c31\u7eea\u961f\u5217\u7b49\u5f85OS\u5206\u914d\u7cfb\u7edf\u8d44\u6e90\u3002sleep(milliseconds)\u53ef\u4ee5\u7528\u65f6\u95f4\u6307\u5b9a\u4f7f\u5b83\u81ea\u52a8\u5524\u9192\u8fc7\u6765\uff0c\u5982\u679c\u65f6\u95f4\u4e0d\u5230\u53ea\u80fd\u8c03\u7528interrupt()\u5f3a\u884c\u6253\u65ad\u3002<\/p>\n
Thread.Sleep(0)\u7684\u4f5c\u7528\u662f\u201c\u89e6\u53d1\u64cd\u4f5c\u7cfb\u7edf\u7acb\u523b\u91cd\u65b0\u8fdb\u884c\u4e00\u6b21CPU\u7ade\u4e89\u201d\u3002<\/p>\n
sleep\u662f\u7ebf\u7a0b\u88ab\u8c03\u7528\u65f6\uff0c\u5360\u7740cpu\u53bb\u7761\u89c9\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u80fd\u5360\u7528cpu\uff0cos\u8ba4\u4e3a\u8be5\u7ebf\u7a0b\u6b63\u5728\u5de5\u4f5c\uff0c\u4e0d\u4f1a\u8ba9\u51fa\u7cfb\u7edf\u8d44\u6e90\uff0cwait\u662f\u8fdb\u5165\u7b49\u5f85\u6c60\u7b49\u5f85\uff0c\u8ba9\u51fa\u7cfb\u7edf\u8d44\u6e90\uff0c\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u5360\u7528cpu\uff0c\u4e00\u822cwait\u4e0d\u4f1a\u52a0\u65f6\u95f4\u9650\u5236\uff0c\u56e0\u4e3a\u5982\u679cwait\u7684\u7ebf\u7a0b\u8fd0\u884c\u8d44\u6e90\u4e0d\u591f\uff0c\u518d\u51fa\u6765\u4e5f\u6ca1\u7528\uff0c\u8981\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528notifyall\u65b9\u6cd5\u5524\u9192\u7b49\u5f85\u6c60\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\uff0c\u624d\u4f1a\u5728\u8fdb\u5165\u5c31\u7eea\u5e8f\u5217\u7b49\u5f85os\u5206\u914d\u7cfb\u7edf\u8d44\u6e90\uff0c
\nsleep\u662f\u9759\u6001\u65b9\u6cd5\uff0c\u662f\u8c01\u6389\u7684\u8c01\u53bb\u7761\u89c9\uff0c\u5c31\u7b97\u662f\u5728main\u7ebf\u7a0b\u91cc\u8c03\u7528\u4e86\u7ebf\u7a0bb\u7684sleep\u65b9\u6cd5\uff0c\u5b9e\u9645\u4e0a\u8fd8\u662fmain\u53bb\u7761\u89c9\uff0c\u60f3\u8ba9\u7ebf\u7a0bb\u53bb\u7761\u89c9\u8981\u5728b\u7684\u4ee3\u7801\u4e2d\u6389sleep<\/p>\n
sleep(100L)\u662f\u5360\u7528cpu\uff0c\u7ebf\u7a0b\u4f11\u7720100\u6beb\u79d2\uff0c\u5176\u4ed6\u8fdb\u7a0b\u4e0d\u80fd\u518d\u5360\u7528cpu\u8d44\u6e90\uff0cwait\uff08100L\uff09\u662f\u8fdb\u5165\u7b49\u5f85\u6c60\u4e2d\u7b49\u5f85\uff0c\u4ea4\u51facpu\u7b49\u7cfb\u7edf\u8d44\u6e90\u4f9b\u5176\u4ed6\u8fdb\u7a0b\u4f7f\u7528\uff0c\u5728\u8fd9100\u6beb\u79d2\u4e2d\uff0c\u8be5\u7ebf\u7a0b\u53ef\u4ee5\u88ab\u5176\u4ed6\u7ebf\u7a0bnotify\uff0c\u4f46\u4e0d\u540c\u7684\u662f\u5176\u4ed6\u5728\u7b49\u5f85\u6c60\u4e2d\u7684\u7ebf\u7a0b\u4e0d\u88abnotify\u4e0d\u4f1a\u51fa\u6765\uff0c\u4f46\u8fd9\u4e2a\u7ebf\u7a0b\u5728\u7b49\u5f85100\u6beb\u79d2\u540e\u4f1a\u81ea\u52a8\u8fdb\u5165\u5c31\u7eea\u961f\u5217\u7b49\u5f85\u7cfb\u7edf\u5206\u914d\u8d44\u6e90\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0csleep\uff08100\uff09\u5728100\u6beb\u79d2\u540e\u80af\u5b9a\u4f1a\u8fd0\u884c\uff0c\u4f46wait\u5728100\u6beb\u79d2\u540e\u8fd8\u6709\u7b49\u5f85os\u8c03\u7528\u5206\u914d\u8d44\u6e90\uff0c\u6240\u4ee5wait100\u7684\u505c\u6b62\u8fd0\u884c\u65f6\u95f4\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u4f46\u81f3\u5c11\u662f100\u6beb\u79d2\u3002<\/p>\n
\u5c31\u662f\u8bf4sleep\u6709\u65f6\u95f4\u9650\u5236\u7684\u5c31\u50cf\u95f9\u949f\u4e00\u6837\u5230\u65f6\u5019\u5c31\u53eb\u4e86\uff0c\u800cwait\u662f\u65e0\u9650\u671f\u7684\u9664\u975e\u7528\u6237\u4e3b\u52a8notify<\/p>\n
wait\uff0cnotify\u548cnotifyAll\u53ea\u80fd\u5728\u540c\u6b65\u63a7\u5236\u65b9\u6cd5\u6216\u8005\u540c\u6b65\u63a7\u5236\u5757\u91cc\u9762\u4f7f\u7528\uff0c\u800csleep\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u4f7f\u7528<\/p>\n
synchronized(x){\r\nx.notify()\r\n\/\/\u6216\u8005wait()\r\n}<\/pre>\n4\u3001\u662f\u5426\u9700\u8981\u6355\u83b7\u5f02\u5e38<\/strong><\/div>\nsleep\u5fc5\u987b\u6355\u83b7\u5f02\u5e38\uff0c\u800cwait\uff0cnotify\u548cnotifyAll\u4e0d\u9700\u8981\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/blog.csdn.net\/catoop\/article\/details\/8305872<\/a><\/p>\n