{"id":10006213,"date":"2022-08-16T14:13:57","date_gmt":"2022-08-16T06:13:57","guid":{"rendered":"https:\/\/ityet.com\/?p=10006213"},"modified":"2022-08-16T14:13:57","modified_gmt":"2022-08-16T06:13:57","slug":"%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e7%9a%84%e4%b8%ad%e6%96%ad%e7%ba%bf%e7%a8%8b%ef%bc%9f%e4%bd%a0%e7%9a%84%e5%a7%bf%e5%8a%bf%e6%98%af%e5%90%a6%e6%ad%a3%e7%a1%ae","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006213","title":{"rendered":"\u5982\u4f55\u6b63\u786e\u7684\u4e2d\u65ad\u7ebf\u7a0b\uff1f\u4f60\u7684\u59ff\u52bf\u662f\u5426\u6b63\u786e"},"content":{"rendered":"<h2>Java\u505c\u6b62\u7ebf\u7a0b\u7684\u903b\u8f91\uff08\u534f\u540c\u3001\u901a\u77e5\uff09<\/h2>\n<p>\u5728Java\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u60f3\u8981\u505c\u6b62\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7interrupt\u65b9\u6cd5\u8fdb\u884c\u505c\u6b62\u3002\u4f46\u662f\u5f53\u6211\u4eec\u8c03\u7528interrupt\u65b9\u6cd5\u4e4b\u540e\uff0c\u5b83\u53ef\u80fd\u5e76\u4e0d\u4f1a\u7acb\u523b\u5c31\u4f1a\u505c\u6b62\u7ebf\u7a0b\uff0c\u800c\u662f\u901a\u77e5\u7ebf\u7a0b\u9700\u8981\u505c\u6b62\u3002\u7ebf\u7a0b\u63a5\u6536\u5230\u901a\u77e5\u4e4b\u540e\u4f1a\u6839\u636e\u81ea\u8eab\u7684\u60c5\u51b5\u5224\u65ad\u662f\u5426\u9700\u8981\u505c\u6b62\uff0c\u5b83\u53ef\u80fd\u4f1a\u7acb\u5373\u505c\u6b62\uff0c\u4e5f\u6709\u53ef\u80fd\u4f1a\u6267\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u505c\u6b62\uff0c\u4e5f\u53ef\u80fd\u6839\u672c\u5c31\u4e0d\u505c\u6b62\u3002<\/p>\n<p>\u90a3\u4e48Java\u4e3a\u4ec0\u4e48\u8981\u9009\u62e9\u8fd9\u79cd\u975e\u5f3a\u5236\u6027\u7684\u7ebf\u7a0b\u4e2d\u65ad\u5462\uff1f\u5176\u5b9e\u66f4\u591a\u662f\u4e3a\u4e86\u6570\u636e\u5b89\u5168\uff0c\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u56e0\u4e3a\u6211\u4eec\u4e0d\u77e5\u9053\u7a0b\u5e8f\u6b63\u5728\u505a\u4ec0\u4e48\u4e8b\u60c5\u3002\u5982\u679c\u8d38\u7136\u505c\u6b62\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u7684\u9519\u4e71\u3001\u4e0d\u5b8c\u6574\u3002<\/p>\n<p><strong>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">public class _24_ThreadTest implements Runnable {\n\n    @Override\n    public void run() {\n        int count = 0;\n        while (!Thread.currentThread().isInterrupted() &amp;&amp; count &lt;= 2000) {\n            System.out.println(&quot;count: &quot; + count++);\n        }\n    }\n\n    public static void main(String[] args) throws Exception {\n        _24_ThreadTest threadTest = new _24_ThreadTest();\n        Thread thread = new Thread(threadTest);\n        thread.start();\n        Thread.sleep(10);\n        \/\/ \u4e2d\u65ad\u7ebf\u7a0b\n        thread.interrupt();\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u901a\u8fc7interrupt\u4e2d\u65ad\u7ebf\u7a0b\u7684\u6848\u4f8b\uff0crun\u65b9\u6cd5\u4e2d\u901a\u8fc7\u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u4e2d\u65ad\uff0c\u5e76\u4e14count\u662f\u5426\u5927\u4e8e2000\u6765\u8fdb\u884c\u5faa\u73af\u3002\u5982\u679c\u7ebf\u7a0b\u4e2d\u65ad\u5219\u9000\u51fa\u5faa\u73af\uff0c\u7ebf\u7a0b\u6267\u884c\u7ed3\u675f\u3002\u8fd9\u79cd\u5c31\u5c5e\u4e8e\u7ebf\u7a0b\u6b63\u5e38\u505c\u6b62\u7684\u60c5\u51b5\u3002<\/p>\n<h2>Sleep\u662f\u5426\u4f1a\u6536\u5230\u7ebf\u7a0b\u4e2d\u65ad\u4fe1\u53f7<\/h2>\n<pre><code class=\"java\">public class _24_ThreadTest implements Runnable {\n\n    @Override\n    public void run() {\n        int count = 0;\n        while (!Thread.currentThread().isInterrupted() &amp;&amp; count &lt;= 2000) {\n            try {\n                System.out.println(&quot;count: &quot; + count++);\n                \/\/ \u5b50\u7ebf\u7a0b\u7761\u7720\n                Thread.sleep(1000 * 2);\n                System.out.println(&quot;\u65b9\u6cd5\u4f53\uff1a&quot; + Thread.currentThread().isInterrupted());\n            } catch (InterruptedException e) {\n                System.out.println(&quot;\u5f02\u5e38\uff1a&quot; + Thread.currentThread().isInterrupted());\n                \/\/ \u7ebf\u7a0b\u4e2d\u65ad\u6807\u5fd7\u4f4d\u88ab\u91cd\u7f6e\u4e3afalse\n                e.printStackTrace();\n            }\n        }\n    }\n\n    public static void main(String[] args) throws Exception {\n        _24_ThreadTest threadTest = new _24_ThreadTest();\n        Thread thread = new Thread(threadTest);\n        thread.start();\n        Thread.sleep(10);\n        \/\/ \u4e2d\u65ad\u7ebf\u7a0b\n        thread.interrupt();\n    }\n}<\/code><\/pre>\n<p>\u5982\u679c\u5728\u5b50\u7ebf\u7a0b\u4e2d\u7761\u7720\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u901a\u8fc7interrupt\u65b9\u6cd5\u8fdb\u884c\u4e2d\u65ad\uff0c\u90a3\u4e48\u5b50\u7ebf\u7a0b\u8fd8\u80fd\u4e0d\u80fd\u6536\u5230\u4e2d\u65ad\u4fe1\u53f7\u3002\u5176\u5b9e\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7ebf\u7a0b\u4e5f\u662f\u53ef\u4ee5\u63a5\u6536\u5230\u4fe1\u53f7\u901a\u77e5\u7684\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f1a\u629b\u51faInterruptedException\uff0c\u5e76\u4e14\u5c06\u7ebf\u7a0b\u4e2d\u65ad\u6807\u5fd7\u4f4d\u8bbe\u7f6e\u4e3afalse\u3002<\/p>\n<p>\u5728\u629b\u51fa\u5f02\u5e38\u540e\uff0c\u7ebf\u7a0b\u6807\u5fd7\u4f4d\u88ab\u8bbe\u7f6e\u4e3afalse\uff0c\u90a3\u4e48\u5728\u4e0b\u6b21\u5faa\u73af\u5224\u65adcount\u6ca1\u6709\u4e3afalse\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u662f\u53ef\u4ee5\u8fdb\u5165\u5faa\u73af\u4f53\u7684\u3002\u8fd9\u4e2a\u65f6\u5019\u7ebf\u7a0b\u5c31\u65e0\u6cd5\u505c\u6b62\u3002<\/p>\n<p><strong>\u6267\u884c\u7ed3\u679c\uff1a<\/strong><br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042320181\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u6848\u4f8b\u573a\u666f<\/strong>\uff1a<\/p>\n<p>\u5728\u8fdb\u884c\u4e00\u4e9b\u540e\u53f0\u4efb\u52a1\u901a\u8fc7\u7ebf\u7a0b\u8dd1\u7684\u65f6\u5019\uff0c\u5982\u679c\u5728\u5faa\u73af\u4e2d\u9047\u5230\u7ebf\u7a0b\u4e2d\u65ad\u5f02\u5e38\uff0c\u6211\u4eec\u9700\u8981\u7ec8\u6b62\u5f53\u524d\u4efb\u52a1\uff0c\u5e76\u4e14\u544a\u8bc9\u5ba2\u6237\u7aef\u5f53\u524d\u4efb\u52a1\u6267\u884c\u5931\u8d25\u7684\u662f\u54ea\u6761\u8bb0\u5f55\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u4e2d\u518d\u6b21\u4e2d\u65ad\u7684\u65b9\u5f0f\u6765\u505c\u6b62\u7ebf\u7a0b\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u5f53\u524d\u51fa\u73b0\u5f02\u5e38\u7684\u8bb0\u5f55\u662f\u54ea\u6761\u3002\u800c\u4e0d\u4f1a\u662f\u63a5\u7740\u6267\u884c\u4e0b\u53bb\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<pre><code class=\"java\">public class _24_ThreadTest implements Runnable {\n\n    @Override\n    public void run() {\n        int count = 0;\n        while (!Thread.currentThread().isInterrupted() &amp;&amp; count &lt;= 2000) {\n            try {\n                System.out.println(&quot;count: &quot; + count++);\n                \/\/ \u5b50\u7ebf\u7a0b\u7761\u7720\n                Thread.sleep(1000 * 2);\n                Thread.currentThread().interrupt();\n                System.out.println(&quot;\u65b9\u6cd5\u4f53\uff1a&quot; + Thread.currentThread().isInterrupted());\n            } catch (InterruptedException e) {\n                \/\/ \u518d\u6b21\u4e2d\u65ad\n                Thread.currentThread().interrupt();\n                System.out.println(&quot;\u5f02\u5e38\uff1a&quot; + Thread.currentThread().isInterrupted());\n                e.printStackTrace();\n            }\n        }\n    }\n\n    public static void main(String[] args) throws Exception {\n        _24_ThreadTest threadTest = new _24_ThreadTest();\n        Thread thread = new Thread(threadTest);\n        thread.start();\n        Thread.sleep(10);\n        \/\/ \u4e2d\u65ad\u7ebf\u7a0b\n        thread.interrupt();\n    }\n}<\/code><\/pre>\n<p>\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c\u5728\u51fa\u73b0\u7ebf\u7a0b\u4e2d\u65ad\u5f02\u5e38\u4e4b\u540e\u7ebf\u7a0b\u4e2d\u65ad\u6807\u5fd7\u4f4d\u4f1a\u88ab\u91cd\u7f6e\u4e3afalse\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u5f02\u5e38\u4e2d\u624b\u52a8\u7684\u518d\u6b21\u4e2d\u65ad\u5f53\u524d\u7ebf\u7a0b\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u5b8c\u5168\u505c\u6b62\u7ebf\u7a0b\u4efb\u52a1\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4e0a\u9762\u6211\u4eec\u7b80\u5355\u4ecb\u7ecd\u4e86\u5982\u4f55\u6b63\u786e\u7684\u505c\u6b62\u7ebf\u7a0b\uff0c\u5982\u679c\u5728\u4ee5\u540e\u7684\u9762\u8bd5\u4e2d\u88ab\u95ee\u5230\u8fd9\u7c7b\u95ee\u9898\uff0c\u90a3\u4e48\u4f60\u662f\u4e0d\u662f\u53ef\u4ee5\u6d41\u7545\u7684\u56de\u7b54\u9762\u8bd5\u5b98\u4e86\u3002<\/p>\n<p>\u5728run\u65b9\u6cd5\u4e2d\u9047\u5230\u5f02\u5e38\uff0c\u6211\u4eec\u662f\u4e0d\u80fd\u76f4\u63a5\u751f\u541e\u7684\uff0c\u4e00\u5b9a\u8981\u505a\u5904\u7406\uff0c\u4f60\u53ef\u4ee5\u662f\u7b80\u5355\u7684\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4e5f\u53ef\u4ee5\u4e2d\u65ad\u7ebf\u7a0b\u3002\u4f46\u5c31\u662f\u4e0d\u80fd\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002<\/p>\n<p>\u5176\u5b9e\u8fd8\u6709\u5176\u4ed6\u7684\u4e00\u4e9b\u65b9\u6cd5\u6765\u505c\u6b62\u7ebf\u7a0b\uff0c\u6bd4\u5982stop()\uff0c\u8fd9\u7c7b\u65b9\u6cd5\u5df2\u88ab\u820d\u5f03\uff0c\u8fd9\u79cd\u5f3a\u5236\u505c\u6b62\u53ef\u80fd\u4f1a\u5f15\u8d77\u7ebf\u7a0b\u7684\u6570\u636e\u5b89\u5168\u95ee\u9898\uff0c\u6240\u4ee5\u5df2\u7ecf\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\u4e86\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042320179\">https:\/\/segmentfault.com\/a\/1190000042320179<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u505c\u6b62\u7ebf\u7a0b\u7684\u903b\u8f91\uff08\u534f\u540c\u3001\u901a\u77e5\uff09 \u5728Java\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u60f3\u8981\u505c\u6b62\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7interrupt\u65b9\u6cd5\u8fdb\u884c [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006213","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006213"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006213\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}