{"id":3877,"date":"2025-07-12T13:44:54","date_gmt":"2025-07-12T05:44:54","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3877"},"modified":"2025-07-12T13:44:54","modified_gmt":"2025-07-12T05:44:54","slug":"%e4%bb%80%e4%b9%88%e6%98%af%e5%ae%88%e6%8a%a4%e7%ba%bf%e7%a8%8b%ef%bc%8c%e5%ae%83%e6%9c%89%e4%bb%80%e4%b9%88%e7%89%b9%e7%82%b9","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/12\/%e4%bb%80%e4%b9%88%e6%98%af%e5%ae%88%e6%8a%a4%e7%ba%bf%e7%a8%8b%ef%bc%8c%e5%ae%83%e6%9c%89%e4%bb%80%e4%b9%88%e7%89%b9%e7%82%b9\/","title":{"rendered":"\u4ec0\u4e48\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5b83\u6709\u4ec0\u4e48\u7279\u70b9"},"content":{"rendered":"\n<p>\u5728 Java \u4e2d\uff0c<strong>\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon Thread\uff09<\/strong> \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u7a0b\uff0c\u4e3b\u8981\u7528\u4e8e<strong>\u4e3a\u5176\u4ed6\u7ebf\u7a0b\u63d0\u4f9b\u670d\u52a1\u6216\u540e\u53f0\u652f\u6301<\/strong>\uff0c\u5b83\u7684\u751f\u547d\u5468\u671f<strong>\u4f9d\u8d56\u4e8e\u5176\u4ed6\u975e\u5b88\u62a4\u7ebf\u7a0b\uff08\u7528\u6237\u7ebf\u7a0b\uff09<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f\u5b88\u62a4\u7ebf\u7a0b\uff1f<\/h2>\n\n\n\n<p>\u5b88\u62a4\u7ebf\u7a0b\u662f JVM \u63d0\u4f9b\u7684\u4e00\u7c7b\u201c\u540e\u53f0\u7ebf\u7a0b\u201d\uff0c\u5f53 Java \u865a\u62df\u673a\u4e2d <strong>\u6240\u6709\u7684\u975e\u5b88\u62a4\u7ebf\u7a0b\uff08\u7528\u6237\u7ebf\u7a0b\uff09\u90fd\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u9000\u51fa<\/strong>\uff0c\u6574\u4e2a JVM \u8fdb\u7a0b\u7ec8\u6b62\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5b88\u62a4\u7ebf\u7a0b\u7684\u5b58\u5728\u610f\u4e49\uff1a\u4e0d\u662f\u4e3a\u4e86\u5b8c\u6210\u4efb\u52a1\uff0c\u800c\u662f\u4e3a\u4e86\u5b88\u62a4\u5176\u4ed6\u7ebf\u7a0b\u6216\u7cfb\u7edf\u8d44\u6e90\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b88\u62a4\u7ebf\u7a0b\u4e0e\u7528\u6237\u7ebf\u7a0b\u7684\u533a\u522b<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon\uff09<\/th><th>\u7528\u6237\u7ebf\u7a0b\uff08User\uff09<\/th><\/tr><\/thead><tbody><tr><td>\u751f\u547d\u5468\u671f<\/td><td>\u968f\u6240\u6709\u7528\u6237\u7ebf\u7a0b\u7ed3\u675f\u800c\u7ec8\u6b62<\/td><td>JVM \u4fdd\u8bc1\u6267\u884c\u5230\u4efb\u52a1\u5b8c\u6210<\/td><\/tr><tr><td>\u662f\u5426\u5f71\u54cd JVM \u9000\u51fa<\/td><td>\u5426\uff0c\u6240\u6709\u7528\u6237\u7ebf\u7a0b\u7ed3\u675f\u540e\u81ea\u52a8\u9000\u51fa<\/td><td>\u662f\uff0c\u53ea\u8981\u6709\u7528\u6237\u7ebf\u7a0b\u5728\uff0cJVM \u7ee7\u7eed\u8fd0\u884c<\/td><\/tr><tr><td>\u5178\u578b\u7528\u9014<\/td><td>\u5783\u573e\u56de\u6536\u7ebf\u7a0b\u3001\u5b9a\u65f6\u8c03\u5ea6\u3001\u65e5\u5fd7\u6e05\u7406<\/td><td>\u4e3b\u4e1a\u52a1\u903b\u8f91\u6267\u884c\u7ebf\u7a0b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5b88\u62a4\u7ebf\u7a0b\u4ee3\u7801\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DaemonExample {\n    public static void main(String&#91;] args) {\n        Thread daemonThread = new Thread(() -&gt; {\n            while (true) {\n                System.out.println(\"\u6211\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u540e\u53f0\u8fd0\u884c\u4e2d...\");\n                try {\n                    Thread.sleep(1000);\n                } catch (InterruptedException ignored) {}\n            }\n        });\n\n        daemonThread.setDaemon(true);  \/\/ \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\n        daemonThread.start();\n\n        \/\/ \u4e3b\u7ebf\u7a0b\u4f11\u7720 3 \u79d2\u540e\u7ed3\u675f\n        try {\n            Thread.sleep(3000);\n            System.out.println(\"\u4e3b\u7ebf\u7a0b\u7ed3\u675f\");\n        } catch (InterruptedException ignored) {}\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\uff08\u7ea6 3 \u6b21\uff09\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6211\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u540e\u53f0\u8fd0\u884c\u4e2d...\n\u6211\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u540e\u53f0\u8fd0\u884c\u4e2d...\n\u6211\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u540e\u53f0\u8fd0\u884c\u4e2d...\n\u4e3b\u7ebf\u7a0b\u7ed3\u675f<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u7acb\u5373\u505c\u6b62\uff0c<strong>JVM \u81ea\u52a8\u9000\u51fa<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b\u7684\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u5fc5\u987b <strong>\u5728 <code>start()<\/code> \u65b9\u6cd5\u8c03\u7528\u4e4b\u524d<\/strong> \u8c03\u7528 <code>setDaemon(true)<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Thread t = new Thread(task);\nt.setDaemon(true); \/\/ \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\nt.start();<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u82e5\u5728\u7ebf\u7a0b\u542f\u52a8\u540e\u518d\u8c03\u7528 <code>setDaemon(true)<\/code>\uff0c\u5c06\u629b\u51fa <code>IllegalThreadStateException<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5b88\u62a4\u7ebf\u7a0b\u7684\u5e38\u89c1\u7528\u9014<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e94\u7528\u573a\u666f<\/th><th>\u793a\u4f8b\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>JVM \u5783\u573e\u56de\u6536<\/td><td><code>GC Thread<\/code> \u672c\u8eab\u662f\u5b88\u62a4\u7ebf\u7a0b<\/td><\/tr><tr><td>\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6<\/td><td><code>ScheduledExecutorService<\/code> \u4e2d\u7684\u7ebf\u7a0b<\/td><\/tr><tr><td>\u65e5\u5fd7\u5904\u7406<\/td><td>\u5f02\u6b65\u65e5\u5fd7\u5237\u76d8\u7ebf\u7a0b<\/td><\/tr><tr><td>\u540e\u53f0\u8d44\u6e90\u6e05\u7406<\/td><td>\u5982\u7f13\u5b58\u8fc7\u671f\u6e05\u7406\u3001session\u6e05\u7406\u7ebf\u7a0b<\/td><\/tr><tr><td>\u5fc3\u8df3\u76d1\u63a7\u7ebf\u7a0b<\/td><td>\u5b9a\u65f6\u53d1\u9001\u5fc3\u8df3\u5305\u7ef4\u6301\u8fde\u63a5\u72b6\u6001<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5b88\u62a4\u7ebf\u7a0b\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0d\u80fd\u4f9d\u8d56\u5b88\u62a4\u7ebf\u7a0b\u5b8c\u6210\u5173\u952e\u4efb\u52a1<\/strong>\uff0c\u5982\u5199\u6587\u4ef6\u3001\u4fdd\u5b58\u6570\u636e\u7b49<br>\u2192 \u56e0\u4e3a JVM \u53ef\u80fd\u5728\u5b83\u6267\u884c\u4e2d\u9014\u5c31\u9000\u51fa\u4e86\u3002<\/li>\n\n\n\n<li><strong>\u5b88\u62a4\u7ebf\u7a0b\u4e0d\u80fd\u518d\u542f\u52a8\u7528\u6237\u7ebf\u7a0b<\/strong><br>\u5b88\u62a4\u7ebf\u7a0b\u53ea\u80fd\u201c\u966a\u4f34\u201d\u7528\u6237\u7ebf\u7a0b\uff0c\u4e0d\u80fd\u5ef6\u957f JVM \u751f\u547d\u5468\u671f\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u6c60\u9ed8\u8ba4\u7ebf\u7a0b\u7c7b\u578b\u4e0d\u662f\u5b88\u62a4\u7ebf\u7a0b<\/strong><br>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u5982\u9700\u540e\u53f0\u6267\u884c\uff0c\u5e94\u624b\u52a8\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff08\u89c1\u4e0b\u6587\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u7ebf\u7a0b\u6c60\u4e2d\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b\u65b9\u5f0f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {\n    public Thread newThread(Runnable r) {\n        Thread t = new Thread(r);\n        t.setDaemon(true); \/\/ \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\n        return t;\n    }\n});<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u5b98\u65b9\u4e0e\u6743\u5a01\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd17 <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Thread.html#setDaemon-boolean-\">Java \u5b98\u65b9\u6587\u6863\uff1a\u5b88\u62a4\u7ebf\u7a0b<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/index.html\">Oracle Java \u5e76\u53d1\u6559\u7a0b &#8211; Daemon Thread<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 \u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\u7b2c 5 \u7ae0\uff1a\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u4e0e\u63a7\u5236<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u9762\u8bd5\u603b\u7ed3\u53e3\u8bc0<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5b88\u62a4\u7ebf\u7a0b\u505a\u540e\u52e4\uff0c\u4e3b\u7ebf\u7a0b\u8d70\u5b83\u5c31\u505c\uff1b\u522b\u8ba9\u5b83\u5e72\u6b63\u4e8b\uff0cJVM \u4e0d\u4fdd\u547d\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\uff0c\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon Thread\uff09 \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u7a0b\uff0c\u4e3b\u8981\u7528\u4e8e\u4e3a\u5176\u4ed6\u7ebf\u7a0b\u63d0\u4f9b\u670d\u52a1\u6216\u540e\u53f0\u652f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3878,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3877"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3877\/revisions"}],"predecessor-version":[{"id":3879,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3877\/revisions\/3879"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3878"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}