{"id":17016,"date":"2025-04-01T19:49:08","date_gmt":"2025-04-01T11:49:08","guid":{"rendered":"https:\/\/92it.top\/?p=17016"},"modified":"2025-04-01T19:49:08","modified_gmt":"2025-04-01T11:49:08","slug":"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:\/\/92it.top\/?p=17016","title":{"rendered":"Wait \u548c Sleep \u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a\u5929<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247503407&amp;idx=1&amp;sn=13d4a9ed88d96a00deff2fc4a212bb74&amp;chksm=96c4e213a1b36b05ca37c5950c7abaaf244923709dd9082a6bf362b237fc84c072f5196ed393&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u732b2\u9762\uff1a Wait \u548c Sleep \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/a><\/p>\n\n\n\n<p>\u4f5c\u4e3a\u4e00\u540d Java \u5f00\u53d1\u8005\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230\u591a\u7ebf\u7a0b\u7684\u90e8\u5206\uff0c<code>wait<\/code>\u00a0\u548c\u00a0<code>sleep<\/code>\u00a0\u662f\u4e24\u4e2a\u7ecf\u5e38\u4f1a\u78b0\u5230\u7684\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u4eec\u770b\u8d77\u6765\u529f\u80fd\u76f8\u4f3c\uff0c\u4f46\u5b9e\u9645\u4e0a\u6709\u7740\u663e\u8457\u7684\u533a\u522b\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u4e24\u8005\u7684\u4e0d\u540c\u4e4b\u5904\uff0c\u5e76\u7406\u89e3\u5b83\u4eec\u80cc\u540e\u7684\u539f\u7406\u3002<\/p>\n\n\n\n<p><strong>1.\u00a0<code>wait<\/code>\u00a0\u4e0e\u00a0<code>sleep<\/code>\u00a0\u7684\u57fa\u672c\u533a\u522b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b&nbsp;<code>wait<\/code>&nbsp;\u548c&nbsp;<code>sleep<\/code>&nbsp;\u7684\u51e0\u4e2a\u4e3b\u8981\u533a\u522b\uff1a<\/p>\n\n\n\n<p><strong>1. \u6240\u5c5e\u7c7b\u4e0d\u540c<\/strong><\/p>\n\n\n\n<ul>\n<li><code>wait()<\/code>\uff1a\u662f\u00a0<code>java.lang.Object<\/code>\u00a0\u7c7b\u7684\u65b9\u6cd5\u3002\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u90fd\u80fd\u8c03\u7528\u00a0<code>wait()<\/code>\u00a0\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><code>sleep()<\/code>\uff1a\u662f\u00a0<code>java.lang.Thread<\/code>\u00a0\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u3002\u5b83\u5c5e\u4e8e\u7ebf\u7a0b\u7c7b\u672c\u8eab\uff0c\u4e0d\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2. \u9501\u7684\u6301\u6709\u60c5\u51b5<\/strong><\/p>\n\n\n\n<ul>\n<li><code>wait()<\/code>\uff1a\u53ea\u80fd\u5728\u540c\u6b65\u65b9\u6cd5\u6216\u540c\u6b65\u4ee3\u7801\u5757\u4e2d\u8c03\u7528\uff0c\u5fc5\u987b\u6301\u6709\u5bf9\u8c61\u7684\u9501\u3002\u8c03\u7528\u00a0<code>wait()<\/code>\u00a0\u540e\uff0c\u7ebf\u7a0b\u4f1a\u91ca\u653e\u9501\u3002<\/li>\n\n\n\n<li><code>sleep()<\/code>\uff1a\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u8c03\u7528\uff0c\u4e0d\u9700\u8981\u6301\u6709\u4efb\u4f55\u9501\u3002\u8c03\u7528\u00a0<code>sleep()<\/code>\u00a0\u65f6\uff0c\u7ebf\u7a0b\u4e0d\u4f1a\u91ca\u653e\u5b83\u6301\u6709\u7684\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>3. \u4e3b\u8981\u7528\u9014<\/strong><\/p>\n\n\n\n<ul>\n<li><code>wait()<\/code>\uff1a\u4e3b\u8981\u7528\u4e8e\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u548c\u534f\u8c03\uff0c\u901a\u5e38\u4e0e\u00a0<code>notify()<\/code>\u00a0\u6216\u00a0<code>notifyAll()<\/code>\u00a0\u4e00\u8d77\u4f7f\u7528\uff0c\u5b9e\u73b0\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u7b49\u3002<\/li>\n\n\n\n<li><code>sleep()<\/code>\uff1a\u4e3b\u8981\u7528\u4e8e\u8ba9\u7ebf\u7a0b\u6682\u505c\u6267\u884c\u4e00\u6bb5\u65f6\u95f4\uff0c\u901a\u5e38\u7528\u4e8e\u63a7\u5236\u6267\u884c\u901f\u5ea6\u6216\u5b9e\u73b0\u5ef6\u65f6\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>4. \u9501\u7684\u91ca\u653e<\/strong><\/p>\n\n\n\n<ul>\n<li><code>wait()<\/code>\uff1a\u8c03\u7528\u540e\u4f1a\u91ca\u653e\u5bf9\u8c61\u7684\u76d1\u89c6\u5668\u9501\uff0c\u8ba9\u5176\u4ed6\u7b49\u5f85\u8be5\u9501\u7684\u7ebf\u7a0b\u6709\u673a\u4f1a\u6267\u884c\u3002<\/li>\n\n\n\n<li><code>sleep()<\/code>\uff1a\u8c03\u7528\u540e\u4e0d\u4f1a\u91ca\u653e\u4efb\u4f55\u9501\uff0c\u7ebf\u7a0b\u5728\u7761\u7720\u671f\u95f4\u4ecd\u7136\u6301\u6709\u6240\u6709\u5df2\u7ecf\u83b7\u53d6\u7684\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2. \u539f\u7406\u5206\u6790<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u7406\u89e3\u00a0<code>wait<\/code>\u00a0\u548c\u00a0<code>sleep<\/code>\u00a0\u7684\u539f\u7406\uff0c\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u638c\u63e1\u5b83\u4eec\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n\n\n\n<p><strong>2.1\u00a0<code>wait()<\/code>\u00a0\u7684\u539f\u7406<\/strong><\/p>\n\n\n\n<p><code>wait()<\/code>&nbsp;\u65b9\u6cd5\u4f7f\u5f53\u524d\u7ebf\u7a0b\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\uff0c\u76f4\u5230\u88ab\u5176\u4ed6\u7ebf\u7a0b\u901a\u8fc7&nbsp;<code>notify()<\/code>&nbsp;\u6216&nbsp;<code>notifyAll()<\/code>&nbsp;\u5524\u9192\uff0c\u6216\u8005\u7b49\u5f85\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u8c03\u7528&nbsp;<code>wait()<\/code>&nbsp;\u524d\uff0c\u7ebf\u7a0b\u5fc5\u987b\u6301\u6709\u5bf9\u8c61\u7684\u76d1\u89c6\u5668\u9501\u3002\u8c03\u7528\u540e\uff0c\u7ebf\u7a0b\u4f1a\u91ca\u653e\u8be5\u9501\uff0c\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u8fdb\u5165\u540c\u6b65\u5757\u6216\u65b9\u6cd5\u6267\u884c\u3002\u5f53\u88ab\u5524\u9192\u540e\uff0c\u7ebf\u7a0b\u4f1a\u91cd\u65b0\u7ade\u4e89\u9501\uff0c\u6709\u673a\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u673a\u5236\u4e3b\u8981\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u534f\u4f5c\u3002\u4f8b\u5982\uff0c\u751f\u4ea7\u8005\u7ebf\u7a0b\u751f\u4ea7\u6570\u636e\u540e\u901a\u8fc7&nbsp;<code>notify()<\/code>&nbsp;\u5524\u9192\u6d88\u8d39\u8005\u7ebf\u7a0b\u8fdb\u884c\u6d88\u8d39\u3002<\/p>\n\n\n\n<p><strong>2.2\u00a0<code>sleep()<\/code>\u00a0\u7684\u539f\u7406<\/strong><\/p>\n\n\n\n<p><code>sleep()<\/code>&nbsp;\u65b9\u6cd5\u8ba9\u5f53\u524d\u7ebf\u7a0b\u6682\u505c\u6267\u884c\u6307\u5b9a\u7684\u65f6\u95f4\uff0c\u4f46\u7ebf\u7a0b\u5728\u6b64\u671f\u95f4\u4e0d\u4f1a\u91ca\u653e\u4efb\u4f55\u9501\u3002\u8c03\u7528&nbsp;<code>sleep()<\/code>&nbsp;\u540e\uff0c\u7ebf\u7a0b\u8fdb\u5165\u4f11\u7720\u72b6\u6001\uff0c\u65f6\u95f4\u5230\u8fbe\u540e\u81ea\u52a8\u6062\u590d\u8fd0\u884c\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e3b\u8981\u7528\u4e8e\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\u8282\u594f\uff0c\u6bd4\u5982\u5b9a\u65f6\u4efb\u52a1\u6216\u6a21\u62df\u5ef6\u65f6\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><code>sleep()<\/code>&nbsp;\u4e0d\u4f9d\u8d56\u4e8e\u540c\u6b65\u673a\u5236\uff0c\u56e0\u6b64\u66f4\u52a0\u7075\u6d3b\uff0c\u4f46\u4e5f\u4e0d\u9002\u5408\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u534f\u4f5c\u3002<\/p>\n\n\n\n<p><strong>3. \u5b9e\u4f8b\u6f14\u793a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e24\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u770b\u770b\u00a0<code>wait()<\/code>\u00a0\u548c\u00a0<code>sleep()<\/code>\u00a0\u5728\u5b9e\u9645\u4e2d\u7684\u4f7f\u7528\u533a\u522b\u3002<\/p>\n\n\n\n<p><strong>3.1 \u4f7f\u7528\u00a0<code>wait()<\/code><\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5171\u4eab\u8d44\u6e90\uff0c\u751f\u4ea7\u8005\u7ebf\u7a0b\u8d1f\u8d23\u751f\u6210\u6570\u636e\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u8d1f\u8d23\u6d88\u8d39\u6570\u636e\u3002\u6211\u4eec\u5e0c\u671b\u5728\u6ca1\u6709\u6570\u636e\u65f6\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u7b49\u5f85\uff1b\u6709\u6570\u636e\u65f6\uff0c\u6d88\u8d39\u8005\u88ab\u5524\u9192\u5e76\u6d88\u8d39\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class ProducerConsumer {\n    privatefinal Object lock = new Object();\n    privateboolean hasData = false;\n\n    public void produce() throws InterruptedException {\n        synchronized (lock) {\n            while (hasData) {\n                lock.wait(); \/\/ \u7b49\u5f85\u6d88\u8d39\u8005\u6d88\u8d39\u6570\u636e\n            }\n            System.out.println(\"\u751f\u4ea7\u6570\u636e...\");\n            hasData = true;\n            lock.notify(); \/\/ \u5524\u9192\u6d88\u8d39\u8005\n        }\n    }\n\n    public void consume() throws InterruptedException {\n        synchronized (lock) {\n            while (!hasData) {\n                lock.wait(); \/\/ \u7b49\u5f85\u751f\u4ea7\u8005\u751f\u4ea7\u6570\u636e\n            }\n            System.out.println(\"\u6d88\u8d39\u6570\u636e...\");\n            hasData = false;\n            lock.notify(); \/\/ \u5524\u9192\u751f\u4ea7\u8005\n        }\n    }\n\n    public static void main(String[] args) {\n        ProducerConsumer pc = new ProducerConsumer();\n\n        \/\/ \u751f\u4ea7\u8005\u7ebf\u7a0b\n        new Thread(() -> {\n            try {\n                while (true) {\n                    pc.produce();\n                    Thread.sleep(1000); \/\/ \u6a21\u62df\u751f\u4ea7\u65f6\u95f4\n                }\n            } catch (InterruptedException e) {\n                Thread.currentThread().interrupt();\n            }\n        }, \"Producer\").start();\n\n        \/\/ \u6d88\u8d39\u8005\u7ebf\u7a0b\n        new Thread(() -> {\n            try {\n                while (true) {\n                    pc.consume();\n                    Thread.sleep(1500); \/\/ \u6a21\u62df\u6d88\u8d39\u65f6\u95f4\n                }\n            } catch (InterruptedException e) {\n                Thread.currentThread().interrupt();\n            }\n        }, \"Consumer\").start();\n    }\n}\n<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>produce()<\/code>\u00a0\u548c\u00a0<code>consume()<\/code>\u00a0\u65b9\u6cd5\u90fd\u5728\u540c\u6b65\u5757\u4e2d\u8c03\u7528\u00a0<code>wait()<\/code>\u00a0\u548c\u00a0<code>notify()<\/code>\uff0c\u5b9e\u73b0\u4e86\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\u7684\u534f\u8c03\u3002\u5f53\u751f\u4ea7\u8005\u751f\u4ea7\u5b8c\u6570\u636e\u540e\uff0c\u901a\u8fc7\u00a0<code>notify()<\/code>\u00a0\u5524\u9192\u7b49\u5f85\u7684\u6d88\u8d39\u8005\uff1b\u6d88\u8d39\u8005\u6d88\u8d39\u5b8c\u6570\u636e\u540e\uff0c\u5524\u9192\u751f\u4ea7\u8005\u7ee7\u7eed\u751f\u4ea7\u3002<\/p>\n\n\n\n<p><strong>3.2 \u4f7f\u7528\u00a0<code>sleep()<\/code><\/strong><\/p>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528\u00a0<code>sleep()<\/code>\u00a0\u7684\u4f8b\u5b50\uff0c\u6765\u6a21\u62df\u5ef6\u65f6\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class SleepExample {\n    public static void main(String[] args) {\n        System.out.println(\"\u4efb\u52a1\u5f00\u59cb\u6267\u884c...\");\n        try {\n            Thread.sleep(2000); \/\/ \u6682\u505c2\u79d2\n        } catch (InterruptedException e) {\n            Thread.currentThread().interrupt();\n        }\n        System.out.println(\"\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff01\");\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u4f60\u4f1a\u53d1\u73b0&#8221;\u4efb\u52a1\u5f00\u59cb\u6267\u884c&#8230;&#8221;\u8f93\u51fa\u540e\uff0c\u7a0b\u5e8f\u6682\u505c\u4e862\u79d2\uff0c\u7136\u540e\u8f93\u51fa&#8221;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff01&#8221;\u3002\u8fd9\u91cc\uff0c<code>sleep()<\/code>\u00a0\u7b80\u5355\u5730\u8ba9\u7ebf\u7a0b\u6682\u505c\u4e86\u4e00\u6bb5\u65f6\u95f4\uff0c\u800c\u4e0d\u6d89\u53ca\u4efb\u4f55\u540c\u6b65\u6216\u7ebf\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n\n\n\n<p><strong>4.\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u5206\u6790\u4e86&nbsp;<code>wait()<\/code>&nbsp;\u548c&nbsp;<code>sleep()<\/code>&nbsp;\u7684\u533a\u522b\uff0c\u901a\u8fc7\u4eca\u5929\u7684\u5206\u4eab\uff0c\u6211\u4eec\u4e86\u89e3\u4e86&nbsp;<code>wait()<\/code>&nbsp;\u548c&nbsp;<code>sleep()<\/code>&nbsp;\u5728 Java \u4e2d\u7684\u533a\u522b\u548c\u5404\u81ea\u7684\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ul>\n<li><strong><code>wait()<\/code><\/strong>\u00a0\u662f\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u534f\u8c03\u4e0e\u901a\u4fe1\uff0c\u5fc5\u987b\u5728\u540c\u6b65\u73af\u5883\u4e0b\u4f7f\u7528\uff0c\u5e76\u4e14\u4f1a\u91ca\u653e\u9501\u3002<\/li>\n\n\n\n<li><strong><code>sleep()<\/code><\/strong>\u00a0\u7528\u4e8e\u8ba9\u7ebf\u7a0b\u6682\u505c\u6267\u884c\u4e00\u6bb5\u65f6\u95f4\uff0c\u4e0d\u6d89\u53ca\u9501\u7684\u91ca\u653e\uff0c\u9002\u7528\u4e8e\u5ef6\u65f6\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u638c\u63e1\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u533a\u522b\u548c\u7528\u6cd5\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u6709\u6548\u5730\u7ba1\u7406\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ebf\u7a0b\u884c\u4e3a\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u5929\u732b2\u9762\uff1a Wait \u548c Sleep \u6709\u4ec0\u4e48\u533a\u522b\uff1f \u4f5c\u4e3a\u4e00\u540d Java \u5f00\u53d1\u8005\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230\u591a\u7ebf\u7a0b\u7684\u90e8\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17016"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17016"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17016\/revisions"}],"predecessor-version":[{"id":17017,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17016\/revisions\/17017"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}