{"id":208291,"date":"2025-07-08T16:51:50","date_gmt":"2025-07-08T08:51:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208291\/"},"modified":"2025-07-08T16:51:50","modified_gmt":"2025-07-08T08:51:50","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e8%ae%a1%e6%97%b6%e5%99%a8-stop-%e4%bc%9a%e5%87%ba%e7%8e%b0%e6%ad%bb%e9%94%81%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208291\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u8ba1\u65f6\u5668 Stop \u4f1a\u51fa\u73b0\u6b7b\u9501\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48\u8ba1\u65f6\u5668 Stop \u4f1a\u51fa\u73b0\u6b7b\u9501\u9519\u8bef\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 12:48:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u8ba1\u65f6\u5668 Stop \u4f1a\u51fa\u73b0\u6b7b\u9501\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u521b\u5efa\u8fd9\u4e2a\u51fd\u6570\u6765\u6d4b\u8bd5\u521b\u5efa\u548c\u505c\u6b62\u8ba1\u65f6\u5668\u3002\u8fd0\u884c\u65f6\u51fa\u73b0\u6b7b\u9501\u9519\u8bef\uff1a<\/p>\n<pre>package main\n\nimport \"fmt\"\nimport \"time\"\n\n\nfunc main() {\n    livenesstimer := &amp;time.timer{}\n    livenessinterval, _ := time.parseduration(\"1m\")\n    for {\n        fmt.print(\"timer started\")\n        livenesstimer = time.newtimer(livenessinterval)\n        select {\n        case &lt;-livenesstimer.c:\n            fmt.print(time.now())\n            fmt.println(\"timer triggered\")\n        }\n        if !livenesstimer.stop() {\n            \/\/ drain timer from channel if any\n            fmt.println(\"drain timer\")\n            &lt;-livenesstimer.c\n        }\n    }\n}<\/pre>\n<p>\u5f53\u6211\u8fd0\u884c\u6b64\u4ee3\u7801\u65f6\uff0c\u51fa\u73b0\u6b64\u9519\u8bef\uff1a<\/p>\n<pre>Timer started2009-11-10 23:01:00 +0000 UTC m=+60.000000001timer triggered\ndrain timer\nfatal error: all goroutines are asleep - deadlock!\n\ngoroutine 1 [chan receive]:\nmain.main()\n    \/tmp\/sandbox748850751\/prog.go:21 +0x2e0<\/pre>\n<p><code>stop<\/code> \u7684\u6587\u6863\u5efa\u8bae\u68c0\u67e5\u8fd4\u56de\u503c\u5e76\u6392\u7a7a\u901a\u9053\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f\u6587\u6863\u4e2d\u7684\u5173\u952e\u90e8\u5206\uff1a<\/p>\n<p>&#8230;\u5047\u8bbe\u7a0b\u5e8f\u5c1a\u672a\u6536\u5230\u6765\u81ea t.C \u7684\u4fe1\u606f<\/p>\n<p>\u5728\u60a8\u7684\u60c5\u51b5\u4e0b\uff0c\u8ba1\u65f6\u5668\u5df2\u7ecf\u89e6\u53d1\uff0c<code>Stop<\/code> \u8fd4\u56de false\uff0c\u5e76\u4e14\u60a8\u5f00\u59cb\u7b49\u5f85\u6ca1\u6709\u5176\u4ed6 goroutine \u8fd0\u884c\u7684\u901a\u9053\uff0c\u56e0\u6b64\u6b7b\u9501\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4e3a\u4ec0\u4e48\u8ba1\u65f6\u5668 Stop \u4f1a\u51fa\u73b0\u6b7b\u9501\u9519\u8bef\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208291","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208291"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208291\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}