{"id":16414,"date":"2024-10-08T10:59:57","date_gmt":"2024-10-08T02:59:57","guid":{"rendered":"https:\/\/92it.top\/?p=16414"},"modified":"2024-10-08T13:16:48","modified_gmt":"2024-10-08T05:16:48","slug":"%e5%ae%88%e6%8a%a4%e7%ba%bf%e7%a8%8b%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16414","title":{"rendered":"\u5b88\u62a4\u7ebf\u7a0b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247499714&amp;idx=1&amp;sn=2be184cb34038910d26df8fb60709f3c&amp;chksm=96c4d3fea1b35ae8777393100e4d64a81b6ba30d5c5ea20d3bd41e3e89d185067d2f9b1c6de4&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u5b88\u62a4\u7ebf\u7a0b\u662f\u4ec0\u4e48\uff1f<\/a><\/p>\n\n\n\n<p>\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon Thread\uff09\u662f\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u7279\u522b\u662f\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u6267\u884c\u67d0\u4e9b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u9700\u8981\u6301\u7eed\u8fd0\u884c\u7684\u540e\u53f0\u4efb\u52a1\u3002\u8fd9\u4e2a\u6982\u5ff5\u6700\u521d\u662f\u5728Java\u8bed\u8a00\u4e2d\u5f15\u5165\u7684\uff0c\u4f46\u540e\u6765\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u6765\u8be6\u7ec6\u8ba8\u8bba\u4e00\u4e0b\u5b88\u62a4\u7ebf\u7a0b\u7684\u7279\u70b9\u3001\u4f7f\u7528\u573a\u666f\u3001\u4f18\u7f3a\u70b9\u3001\u4ee5\u53ca\u4e00\u4e9b\u76f8\u5173\u7684\u6280\u672f\u7ec6\u8282\u3002<\/p>\n\n\n\n<p><strong>\u5b9a\u4e49\u4e0e\u7279\u70b9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ol>\n<li><strong>\u540e\u53f0\u8fd0\u884c<\/strong>\uff1a\u5b88\u62a4\u7ebf\u7a0b\u901a\u5e38\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u9ed8\u9ed8\u5730\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u670d\u52a1\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u6267\u884c\u65e0\u987b\u7528\u6237\u76f4\u63a5\u4ea4\u4e92\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u76d1\u63a7\u8d44\u6e90\u3001\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u3001\u5783\u573e\u56de\u6536\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u751f\u547d\u5468\u671f<\/strong>\uff1a\u5b88\u62a4\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4e0e\u7a0b\u5e8f\u7684\u4e3b\u7ebf\u7a0b\u5bc6\u5207\u76f8\u5173\u3002\u5f53\u6240\u6709\u7684\u975e\u5b88\u62a4\u7ebf\u7a0b\uff08\u5373\u7528\u6237\u7ebf\u7a0b\uff09\u7ed3\u675f\u65f6\uff0c\u865a\u62df\u673a\u4f1a\u81ea\u52a8\u9000\u51fa\u8fd0\u884c\uff0c\u4e0d\u7ba1\u5b88\u62a4\u7ebf\u7a0b\u662f\u5426\u4ecd\u5728\u8fd0\u884c\u3002\u56e0\u6b64\uff0c\u5b88\u62a4\u7ebf\u7a0b\u65e0\u6cd5\u963b\u6b62\u5e94\u7528\u7a0b\u5e8f\u7684\u9000\u51fa\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u5148\u7ea7\u4f4e<\/strong>\uff1a\u7531\u4e8e\u5b83\u4eec\u4e3b\u8981\u7528\u4e8e\u63d0\u4f9b\u4e00\u4e9b\u670d\u52a1\u529f\u80fd\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e00\u822c\u88ab\u8bbe\u7f6e\u4e3a\u8f83\u4f4e\u7684\u4f18\u5148\u7ea7\u3002\u8fd9\u786e\u4fdd\u4e86\u5b83\u4eec\u4e0d\u4f1a\u4e0e\u7528\u6237\u7ebf\u7a0b\u62a2\u5360\u8d44\u6e90\u3002\u5982\u679c\u7cfb\u7edf\u8d44\u6e90\u7d27\u5f20\uff0c\u5b88\u62a4\u7ebf\u7a0b\u53ef\u80fd\u5c31\u5f97\u4e0d\u5230\u53ca\u65f6\u7684\u8c03\u5ea6\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u5982\u4f55\u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\u975e\u5e38\u7b80\u5355\uff0c\u4f60\u53ea\u9700\u8981\u5728\u542f\u52a8\u7ebf\u7a0b\u4e4b\u524d\u8c03\u7528\u7ebf\u7a0b\u5b9e\u4f8b\u7684 <code>setDaemon(true)<\/code> \u65b9\u6cd5\uff1a<\/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=\"\">Thread thread = new Thread(() -> {\n  \/\/ \u4ee3\u7801\u7701\u7565\n});\nthread.setDaemon(true);\nthread.start();<\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5fc5\u987b\u5728\u8c03\u7528 <code>start()<\/code> \u65b9\u6cd5\u4e4b\u524d\u8bbe\u7f6e\u7ebf\u7a0b\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5426\u5219\u4f1a\u629b\u51fa <code>IllegalThreadStateException<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u5982\u4f55\u5173\u95ed\u5b88\u62a4\u7ebf\u7a0b\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5173\u95ed\u5b88\u62a4\u7ebf\u7a0b\u901a\u5e38\u662f\u4e0d\u9700\u8981\u663e\u5f0f\u8fdb\u884c\u7684\uff0c\u56e0\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u7684\u8bbe\u8ba1\u76ee\u7684\u5c31\u662f\u5728\u6240\u6709\u975e\u5b88\u62a4\u7ebf\u7a0b\u5b8c\u6210\u540e\u81ea\u52a8\u7ec8\u6b62\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6216\u5e0c\u671b\u624b\u52a8\u63a7\u5236\u5b88\u62a4\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u4fbf\u4e8e\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\u6216\u6e05\u7406\u64cd\u4f5c\u7684\u5b8c\u6210\u3002\u8fd9\u91cc\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u548c\u5173\u95ed\u5b88\u62a4\u7ebf\u7a0b\uff1a<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u4e00\u4e2a\u5171\u4eab\u7684\u6807\u5fd7\u53d8\u91cf\uff0c\u8ba9\u7ebf\u7a0b\u5728\u7b26\u5408\u6761\u4ef6\u65f6\u81ea\u884c\u7ed3\u675f\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=\"\">Thread thread = new Thread(() -> {\n    while (running) {\n    }\n});\n\nthread.setDaemon(true);\nthread.start();\n\n\/\/ \u6a21\u62df\u4e3b\u7ebf\u7a0b\u5de5\u4f5c\nThread.sleep(5000);\nrunning = false; \/\/ \u901a\u77e5\u5b88\u62a4\u7ebf\u7a0b\u7ec8\u6b62<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u8bbe\u7f6e <code>running<\/code> \u6807\u5fd7\u53d8\u91cf\u4e3a <code>false<\/code>\uff0c\u53ef\u4ee5\u901a\u77e5\u5b88\u62a4\u7ebf\u7a0b\u7ed3\u675f\u5176\u5de5\u4f5c\u5faa\u73af\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u7ebf\u7a0b\u7684\u63a7\u5236\u548c\u5173\u95ed\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528<code>interrupt<\/code>\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 <code>interrupt<\/code> \u6765\u4e2d\u65ad\u7ebf\u7a0b\u4e5f\u662f\u4e00\u79cd\u65b9\u6cd5\u3002\u867d\u7136 <code>interrupt<\/code> \u65b9\u6cd5\u5e76\u4e0d\u4f1a\u76f4\u63a5\u5173\u95ed\u7ebf\u7a0b\uff0c\u4f46\u5b83\u4f1a\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u4e2d\u65ad\u72b6\u6001\uff0c\u5e76\u53ef\u4ee5\u7528\u6765\u4e2d\u65ad\u6b63\u5904\u4e8e <code>wait<\/code>\u3001<code>sleep<\/code> \u6216 <code>join<\/code> \u72b6\u6001\u7684\u7ebf\u7a0b\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=\"\">Thread thread = new Thread(() -> { });\nthread.setDaemon(true);\nthread.start();\n\n\/\/ \u6a21\u62df\u4e3b\u7ebf\u7a0b\u7684\u5de5\u4f5c\nThread.sleep(5000);\nthread.interrupt(); \/\/ \u8bf7\u6c42\u5b88\u62a4\u7ebf\u7a0b\u4e2d\u65ad<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528 <code>interrupt()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bf7\u6c42\u5b88\u62a4\u7ebf\u7a0b\u7ec8\u6b62\u6267\u884c\u3002\u7ebf\u7a0b\u4f1a\u6355\u83b7\u5230 <code>InterruptedException<\/code> \u5e76\u5728\u5176\u5904\u7406\u4ee3\u7801\u4e2d\u4ece\u5faa\u73af\u9000\u51fa\u3002<\/p>\n\n\n\n<p><strong>\u8d44\u6e90\u81ea\u52a8\u7ba1\u7406<\/strong><\/p>\n\n\n\n<p>\u6709\u65f6\u5019\uff0c\u5b88\u62a4\u7ebf\u7a0b\u53ef\u80fd\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u8d44\u6e90\uff0c\u5982\u679c\u8fd9\u4e9b\u8d44\u6e90\u4e0d\u518d\u53ef\u7528\uff0c\u7ebf\u7a0b\u81ea\u7136\u4e5f\u5e94\u8be5\u7ed3\u675f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\u6b63\u5728\u5904\u7406\u7f51\u7edc\u8fde\u63a5\uff0c\u5f53\u8fde\u63a5\u5173\u95ed\u65f6\uff0c\u53ef\u4ee5\u7ed3\u675f\u7ebf\u7a0b\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=\"\">try (ServerSocket serverSocket = new ServerSocket(8080)) {\n    Thread thread = new Thread(() -> { \/\/ \u5176\u4ed6\u4ee3\u7801 });\n    thread.setDaemon(true);\n    thread.start();\n\n    \/\/ \u6a21\u62df\u4e3b\u7ebf\u7a0b\u5de5\u4f5c\n    Thread.sleep(5000);\n} catch(Exception e){}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8d44\u6e90\u81ea\u52a8\u7ba1\u7406\u6765\u5173\u95ed\u5b88\u62a4\u7ebf\u7a0b\u3002\u5f53 <code>ServerSocket<\/code> \u88ab\u5173\u95ed\u65f6\uff0c\u63a5\u4e0b\u6765\u7684 <code>accept()<\/code> \u8c03\u7528\u5c06\u5931\u8d25\uff0c\u5bfc\u81f4\u7ebf\u7a0b\u7ec8\u6b62\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u4e13\u95e8\u7684\u7ebf\u7a0b\u6c60\u7ba1\u7406<\/strong><\/p>\n\n\n\n<p>\u5bf9\u4e8e\u66f4\u4e3a\u590d\u6742\u7684\u5e94\u7528\uff0c\u7279\u522b\u662f\u6d89\u53ca\u5230\u591a\u4e2a\u5b88\u62a4\u7ebf\u7a0b\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002Java \u63d0\u4f9b\u4e86 <code>ExecutorService<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 <code>shutdown()<\/code> \u6216 <code>shutdownNow()<\/code> \u65b9\u6cd5\u6765\u7ec8\u6b62\u7ebf\u7a0b\u6c60\u4e2d\u8fd0\u884c\u7684\u7ebf\u7a0b\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=\"\">ExecutorService executorService = Executors.newSingleThreadExecutor(r -> {\n    Thread t = new Thread(r);\n    t.setDaemon(true);\n    return t;\n});\n\nexecutorService.submit(() -> { });\n\n\/\/ \u8bf7\u6c42\u5173\u95ed\u7ebf\u7a0b\u6c60\nexecutorService.shutdown();\ntry {\n    if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {\n        executorService.shutdownNow();\n    }\n} catch (InterruptedException e) {\n    executorService.shutdownNow();\n}<\/pre>\n\n\n\n<p>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u597d\u5904\u662f\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u591a\u4e2a\u4efb\u52a1\u7684\u6267\u884c\u548c\u7ec8\u6b62\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u91cd\u7528\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p><strong>\u5b88\u62a4\u7ebf\u7a0b\u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ol>\n<li><strong>\u5783\u573e\u56de\u6536<\/strong>\uff1a\u5728Java\u4e2d\uff0c\u5783\u573e\u56de\u6536\u7ebf\u7a0b\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5b88\u62a4\u7ebf\u7a0b\u3002\u5b83\u6301\u7eed\u76d1\u63a7\u5bf9\u8c61\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u56de\u6536\u4e0d\u518d\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u4ee5\u91ca\u653e\u5185\u5b58\u3002<\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u67d0\u4e9b\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u5e0c\u671b\u6301\u7eed\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\u5230\u6587\u4ef6\u6216\u5916\u90e8\u7cfb\u7edf\uff0c\u8fd9\u79cd\u8bb0\u5f55\u64cd\u4f5c\u53ef\u4ee5\u7531\u5b88\u62a4\u7ebf\u7a0b\u6765\u5904\u7406\uff0c\u4ee5\u4fbf\u4e0d\u4f1a\u5e72\u6270\u4e3b\u4e1a\u52a1\u903b\u8f91\u3002<\/li>\n\n\n\n<li><strong>\u5fc3\u8df3\u673a\u5236<\/strong>\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5b88\u62a4\u7ebf\u7a0b\u5e38\u5e38\u7528\u4e8e\u5b9e\u73b0\u5fc3\u8df3\u673a\u5236\uff0c\u4ee5\u76d1\u63a7\u7cfb\u7edf\u7ec4\u4ef6\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6<\/strong>\uff1a\u5b88\u62a4\u7ebf\u7a0b\u53ef\u4ee5\u7528\u4e8e\u8c03\u5ea6\u548c\u6267\u884c\u4e00\u4e9b\u5b9a\u65f6\u4efb\u52a1\uff0c\u5982\u5b9a\u65f6\u6e05\u7406\u3001\u6570\u636e\u540c\u6b65\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u540e\u53f0\u8ba1\u7b97<\/strong>\uff1a\u4e00\u4e9b\u8017\u65f6\u7684\u8ba1\u7b97\u6216\u8005\u9700\u8981\u6301\u7eed\u8fd0\u884c\u7684\u540e\u53f0\u8ba1\u7b97\u4e5f\u9002\u5408\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>ChatGPT\u7684\u89e3\u91ca<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c<strong>\u5b88\u62a4\u7ebf\u7a0b<\/strong>\uff08Daemon Thread\uff09\u548c<strong>\u7528\u6237\u7ebf\u7a0b<\/strong>\uff08\u6b63\u5e38\u7ebf\u7a0b\uff09\u6709\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u533a\u522b\uff1a<\/p>\n\n\n\n<p>1. <strong>\u751f\u547d\u5468\u671f<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u7528\u6237\u7ebf\u7a0b\uff08\u6b63\u5e38\u7ebf\u7a0b\uff09<\/strong>\uff1a\u7528\u6237\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002JVM\uff08Java Virtual Machine\uff09\u53ea\u6709\u5728\u6240\u6709\u7528\u6237\u7ebf\u7a0b\u90fd\u7ed3\u675f\u540e\u624d\u4f1a\u9000\u51fa\u3002<\/li>\n\n\n\n<li><strong>\u5b88\u62a4\u7ebf\u7a0b<\/strong>\uff1a\u5b88\u62a4\u7ebf\u7a0b\u662f\u4e00\u79cd\u8f85\u52a9\u7ebf\u7a0b\uff0c\u5b83\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u7528\u6237\u7ebf\u7a0b\u63d0\u4f9b\u652f\u6301\u670d\u52a1\u3002\u4e00\u65e6\u6240\u6709\u7684\u7528\u6237\u7ebf\u7a0b\u7ed3\u675f\uff0cJVM \u4f1a\u81ea\u52a8\u9000\u51fa\uff0c\u5373\u4f7f\u5b88\u62a4\u7ebf\u7a0b\u8fd8\u5728\u8fd0\u884c\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u4f1a\u88ab\u5f3a\u5236\u505c\u6b62\u3002<\/li>\n<\/ul>\n\n\n\n<p>2. <strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u7528\u6237\u7ebf\u7a0b<\/strong>\uff1a\u9002\u5408\u7528\u4e8e\u6267\u884c\u9700\u8981\u72ec\u7acb\u5b8c\u6210\u7684\u4efb\u52a1\uff0c\u6bd4\u5982\u8ba1\u7b97\u3001\u5904\u7406\u4e1a\u52a1\u903b\u8f91\u7b49\u3002\u901a\u5e38\u662f\u5e94\u7528\u7684\u6838\u5fc3\u903b\u8f91\u6240\u5728\u3002<\/li>\n\n\n\n<li><strong>\u5b88\u62a4\u7ebf\u7a0b<\/strong>\uff1a\u9002\u5408\u7528\u4e8e\u540e\u53f0\u4efb\u52a1\uff0c\u6bd4\u5982\u5783\u573e\u56de\u6536\u3001\u5185\u5b58\u7ba1\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u76d1\u63a7\u7b49\u4e0d\u9700\u8981\u4e3b\u52a8\u63a7\u5236\u7ed3\u675f\u7684\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p>3. <strong>JVM \u9000\u51fa\u6761\u4ef6<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u7528\u6237\u7ebf\u7a0b<\/strong>\uff1a\u53ea\u8981\u6709\u4e00\u4e2a\u7528\u6237\u7ebf\u7a0b\u8fd8\u5728\u8fd0\u884c\uff0cJVM \u5c31\u4e0d\u4f1a\u9000\u51fa\uff0c\u5373\u4f7f\u6240\u6709\u7684\u5b88\u62a4\u7ebf\u7a0b\u90fd\u5df2\u7ecf\u5b8c\u6210\u6216\u7ec8\u6b62\u3002<\/li>\n\n\n\n<li><strong>\u5b88\u62a4\u7ebf\u7a0b<\/strong>\uff1a\u5f53\u6240\u6709\u7684\u7528\u6237\u7ebf\u7a0b\u90fd\u7ed3\u675f\u65f6\uff0cJVM \u4f1a\u9000\u51fa\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u4f1a\u88ab\u5f3a\u5236\u7ec8\u6b62\u3002<\/li>\n<\/ul>\n\n\n\n<p>4. <strong>\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<ul>\n<li>\u4f7f\u7528 <code>setDaemon(true)<\/code> \u6765\u5c06\u7ebf\u7a0b\u6807\u8bb0\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u3002\u5fc5\u987b\u5728\u542f\u52a8\u7ebf\u7a0b\u4e4b\u524d\u8c03\u7528\u6b64\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/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=\"\">Thread thread = new Thread(() -> {\n    \/\/ \u7ebf\u7a0b\u903b\u8f91\n});\nthread.setDaemon(true);  \/\/ \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\nthread.start();<\/pre>\n\n\n\n<p>5. <strong>\u4f8b\u5b50<\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\u7528\u6765\u4fdd\u5b58\u65e5\u5fd7\u6587\u4ef6\uff0c\u7528\u6237\u7ebf\u7a0b\u5728\u5904\u7406\u67d0\u79cd\u4e1a\u52a1\u903b\u8f91\uff1a<\/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 DaemonThreadExample {\n    public static void main(String[] args) {\n        Thread daemonThread = new Thread(() -> {\n            while (true) {\n                System.out.println(\"\u5b88\u62a4\u7ebf\u7a0b\u6b63\u5728\u8fd0\u884c...\");\n                try {\n                    Thread.sleep(1000);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n        });\n        daemonThread.setDaemon(true);  \/\/ \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\n        daemonThread.start();\n\n        \/\/ \u7528\u6237\u7ebf\u7a0b\n        System.out.println(\"\u7528\u6237\u7ebf\u7a0b\u6b63\u5728\u8fd0\u884c...\");\n        try {\n            Thread.sleep(3000);  \/\/ \u7528\u6237\u7ebf\u7a0b\u8fd0\u884c3\u79d2\u540e\u7ed3\u675f\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(\"\u7528\u6237\u7ebf\u7a0b\u7ed3\u675f\");\n    }\n}\n<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u6bcf\u9694\u4e00\u79d2\u6253\u5370\u4e00\u884c\u65e5\u5fd7\uff0c\u4f46\u662f\u5f53\u7528\u6237\u7ebf\u7a0b\u7ed3\u675f\u540e\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u4f1a\u88ab\u5f3a\u5236\u505c\u6b62\uff0cJVM \u968f\u4e4b\u9000\u51fa\u3002<\/p>\n\n\n\n<p>6. <strong>\u4f18\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5b88\u62a4\u7ebf\u7a0b<\/strong>\u9002\u5408\u7528\u4e8e\u6267\u884c\u4e0d\u9700\u8981\u660e\u786e\u7ba1\u7406\u7684\u540e\u53f0\u4efb\u52a1\uff0c\u4f46\u4f60\u4e0d\u80fd\u4f9d\u8d56\u5b88\u62a4\u7ebf\u7a0b\u6267\u884c\u5173\u952e\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u5728 JVM \u9000\u51fa\u65f6\u7a81\u7136\u7ec8\u6b62\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u5b88\u62a4\u7ebf\u7a0b\u6301\u6709\u4efb\u4f55\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u53e5\u67c4\u6216\u7f51\u7edc\u8fde\u63a5\uff09\uff0c\u5728 JVM \u9000\u51fa\u65f6\u5b83\u4eec\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u91ca\u653e\uff0c\u56e0\u6b64\u9700\u8981\u7279\u522b\u5c0f\u5fc3\u8d44\u6e90\u7684\u7ba1\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u603b\u7ed3\u6765\u8bf4\uff0c\u5b88\u62a4\u7ebf\u7a0b\u662f Java \u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u7ebf\u7a0b\uff0c\u7528\u4e8e\u8f85\u52a9\u7528\u6237\u7ebf\u7a0b\uff0c\u5e76\u4e14\u4e0d\u4f1a\u963b\u6b62 JVM \u9000\u51fa\u3002<\/p>\n\n\n\n<p><strong>\u4f18\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u81ea\u52a8\u9000\u51fa<\/strong>\uff1a\u5b88\u62a4\u7ebf\u7a0b\u4e0d\u4f1a\u963b\u6b62JVM\u9000\u51fa\uff0c\u8fd9\u4f7f\u5f97\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7a0b\u5e8f\u53ef\u4ee5\u66f4\u4f18\u96c5\u5730\u505c\u6b62\u8fd0\u884c\uff0c\u800c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u505c\u6b62\u6240\u6709\u540e\u53f0\u4efb\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u8d44\u6e90\u7ba1\u7406<\/strong>\uff1a\u901a\u8fc7\u8ba9\u540e\u53f0\u670d\u52a1\u5728\u5b88\u62a4\u7ebf\u7a0b\u4e2d\u8fd0\u884c\uff0c\u8d44\u6e90\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u7ba1\u7406\u548c\u8c03\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u7b80\u5355\u5b9e\u73b0<\/strong>\uff1a\u901a\u8fc7\u7b80\u5355\u7684\u6807\u8bb0\u5373\u53ef\u5c06\u7ebf\u7a0b\u8f6c\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u6a21\u5f0f\uff0c\u4f7f\u7528\u65b9\u4fbf\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u6570\u636e\u635f\u574f<\/strong>\uff1a\u7531\u4e8e\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u5728\u6240\u6709\u7528\u6237\u7ebf\u7a0b\u7ed3\u675f\u65f6\u7a81\u7136\u88ab\u7ec8\u6b62\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5c1a\u672a\u5b8c\u6210\u7684\u4efb\u52a1\u88ab\u5f3a\u884c\u4e2d\u65ad\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u7684\u4e0d\u4e00\u81f4\u6216\u635f\u574f\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u9002\u5408\u5173\u952e\u4efb\u52a1<\/strong>\uff1a\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u968f\u65f6\u5728\u6ca1\u6709\u9884\u8b66\u7684\u72b6\u6001\u4e0b\u88ab\u7ec8\u6b62\uff0c\u4e0d\u9002\u5408\u7528\u6765\u5904\u7406\u5173\u952e\u4efb\u52a1\u6216\u9700\u8981\u786e\u4fdd\u6267\u884c\u5b8c\u6210\u7684\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u8bd5\u56f0\u96be<\/strong>\uff1a\u7531\u4e8e\u5176\u540e\u53f0\u8fd0\u884c\u7684\u7279\u6027\uff0c\u8c03\u8bd5\u548c\u6392\u67e5\u95ee\u9898\u53ef\u80fd\u53d8\u5f97\u66f4\u5177\u6311\u6218\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5b88\u62a4\u7ebf\u7a0b\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u7684\u89d2\u8272\uff0c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u7075\u6d3b\u548c\u65b9\u4fbf\u7684\u540e\u53f0\u670d\u52a1\u3002\u5c3d\u7ba1\u4e0e\u7528\u6237\u7ebf\u7a0b\u76f8\u6bd4\u6709\u5176\u5c40\u9650\u6027\uff0c\u4f46\u5b83\u4eec\u5728\u5408\u9002\u7684\u573a\u666f\u4e0b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u65f6\uff0c\u9700\u8981\u4ed4\u7ec6\u8003\u8651\u4efb\u52a1\u7684\u91cd\u8981\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4ee5\u907f\u514d\u56e0\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u7684\u63d0\u524d\u7ec8\u6b62\u5bf9\u5e94\u7528\u7a0b\u5e8f\u9020\u6210\u8d1f\u9762\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u5b88\u62a4\u7ebf\u7a0b\u662f\u4ec0\u4e48\uff1f \u5b88\u62a4\u7ebf\u7a0b\uff08Daemon Thread\uff09\u662f\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u7279\u522b\u662f\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b [&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\/16414"}],"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=16414"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16414\/revisions"}],"predecessor-version":[{"id":16417,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16414\/revisions\/16417"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}