{"id":8363,"date":"2024-06-12T08:01:01","date_gmt":"2024-06-12T00:01:01","guid":{"rendered":""},"modified":"2024-06-12T08:01:01","modified_gmt":"2024-06-12T00:01:01","slug":"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8363.html","title":{"rendered":"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b"},"content":{"rendered":"

\u5728Java\u4e2d\u505c\u6b62\u4e00\u4e2a\u7ebf\u7a0b\u6709\u4e09\u79cd\u529e\u6cd5 \uff1a<\/p>\n

1.\u6b63\u5e38\u7ed3\u675f\u6267\u884c\uff1b<\/p>\n

2.\u53d1\u751f\u5f02\u5e38;<\/p>\n

3.\u88ab\u5176\u4ed6\u7ebf\u7a0bstop(Java\u5b98\u65b9\u4e0d\u5efa\u8bae)<\/p>\n

\u53c2\u8003\uff1ahttps:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/concurrency\/threadPrimitiveDeprecation.html<\/p>\n

\u4e3a\u4ec0\u4e48Thread.stop<\/code>\u5f03\u7528\uff1f<\/span><\/h4>\n

\u56e0\u4e3a\u5b83\u672c\u8d28\u4e0a\u662f\u4e0d\u5b89\u5168\u7684\u3002\u505c\u6b62\u7ebf\u7a0b\u4f1a\u5bfc\u81f4\u5b83\u89e3\u9501\u5df2\u9501\u5b9a\u7684\u6240\u6709\u76d1\u89c6\u5668\u3002\uff08\u5f53ThreadDeath<\/code>\u5f02\u5e38\u4f20\u64ad\u5230\u5806\u6808\u4e2d\u65f6\uff0c\u76d1\u89c6\u5668\u5c06\u88ab\u89e3\u9501\u3002\uff09\u5982\u679c\u5148\u524d\u53d7\u8fd9\u4e9b\u76d1\u89c6\u5668\u4fdd\u62a4\u7684\u4efb\u4f55\u5bf9\u8c61\u5904\u4e8e\u4e0d\u4e00\u81f4\u72b6\u6001\uff0c\u5219\u5176\u4ed6\u7ebf\u7a0b\u73b0\u5728\u53ef\u4ee5\u4ee5\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\u67e5\u770b\u8fd9\u4e9b\u5bf9\u8c61\u3002\u636e\u8bf4\u8fd9\u4e9b\u7269\u4f53\u5df2\u88ab \u635f\u574f<\/em>\u3002\u5f53\u7ebf\u7a0b\u5bf9\u53d7\u635f\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4efb\u610f\u884c\u4e3a\u3002\u8fd9\u79cd\u884c\u4e3a\u53ef\u80fd\u5f88\u5fae\u5999\u5e76\u4e14\u96be\u4ee5\u68c0\u6d4b\uff0c\u6216\u8005\u53ef\u80fd\u662f\u660e\u663e\u7684\u3002\u4e0e\u5176\u4ed6\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\u4e0d\u540c\uff0c\u5b83\u4f1a ThreadDeath<\/code>\u9ed8\u9ed8\u5730\u6740\u6b7b\u7ebf\u7a0b; \u56e0\u6b64\uff0c\u7528\u6237\u6ca1\u6709\u8b66\u544a\u4ed6\u7684\u7a0b\u5e8f\u53ef\u80fd\u88ab\u7834\u574f\u3002<\/span><\/p>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/span><\/p>\n

 <\/p>\n

\u6240\u4ee5\u5982\u679c\u9047\u5230\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\u67d0\u4e00\u4e2a\u7ebf\u7a0bA\u4f1a\u4e00\u76f4\u6267\u884c\u4e0b\u53bb\u505c\u4e0d\u4e0b\u6765\uff0c\u8fd9\u79cd\u60c5\u51b5\u662f\u5b58\u5728\u7684\u6bd4\u5982\u90a3\u79cd\u9700\u8981\u6301\u7eed\u53d6\u6837\u7684\u7ebf\u7a0bA\uff0c\u5f53\u7136\u4e86\u5728\u6b63\u5e38\u4ee3\u7801\u91cc\u4f1a\u6709\u201c\u505c\u6b62\u201d\u529f\u80fd\uff0c\u5916\u90e8\u7ebf\u7a0bB\u53ef\u4ee5\u53d1\u9001\u505c\u6b62\u4fe1\u53f7\u7ed9A\uff0cA\u53ef\u4ee5\u76f4\u63a5\u7ed3\u675f\u3002<\/p>\n

\u5982\u679cA\u7ebf\u7a0b\u6ca1\u6709\u8fd9\u79cd\u4fe1\u53f7\u91cf\u90a3\u4e48B\u7ebf\u7a0b\u8fd8\u53ef\u4ee5\u4e3b\u52a8\u505c\u6b62\u4ed6\u4e48\uff1f\u7b54\u6848\u662f\u4e0d\u53ef\u4ee5\uff01<\/p>\n

public class Test { public static void main(String args[]) throws InterruptedException { Thread thread1 = new Thread() { public void run() { fun_a(); } }; thread1.start(); int a = 0; while (a < 100) { Thread.sleep(1000); a++; if (a == 3) { a = 100; thread1.interrupt(); \/\/thread1.stop(); \/\/throw new RuntimeException(\"\u4e3b\u51fd\u6570\u629b\u51fa\u5f02\u5e38\"); } } } public static void fun_a() { for (; ; ) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(\">> \" + DateUtil.getNowTimeString()); } } } <\/code><\/pre>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

 \u53ef\u4ee5\u770b\u5230interrupt\u5e76\u4e0d\u80fd\u8ba9\u8fd0\u884c\u4e2d\u7684\u7ebf\u7a0b\u505c\u6b62\uff0c\u8fd9\u4e2a\u662f\u5f88\u5bb9\u6613\u88ab\u8bef\u89e3\u7684\u5730\u65b9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f5c\u7528\u5e76\u4e0d\u662f\u4e2d\u65ad\u7ebf\u7a0b\uff0c\u800c\u662f\u8bbe\u7f6e\u4e00\u4e2a\u6807\u8bc6\uff0c\u901a\u77e5\u8be5\u7ebf\u7a0b\u53ef\u4ee5\u88ab\u4e2d\u65ad\u4e86\uff0c\u5230\u5e95\u662f\u7ee7\u7eed\u6267\u884c\uff0c\u8fd8\u662f\u4e2d\u65ad\u8fd4\u56de\uff0c\u7531\u7ebf\u7a0b\u672c\u8eab\u81ea\u5df1\u51b3\u5b9a\u3002<\/p>\n

\u5f53\u5bf9\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528\u4e86interrupt()<\/code>\u4e4b\u540e\uff0c\u5982\u679c\u8be5\u7ebf\u7a0b\u5904\u4e8e\u88ab\u963b\u585e\u72b6\u6001\uff08\u6bd4\u5982\u6267\u884c\u4e86wait\u3001sleep\u6216join\u7b49\u65b9\u6cd5\uff09\uff0c\u90a3\u4e48\u4f1a\u7acb\u5373\u9000\u51fa\u963b\u585e\u72b6\u6001\uff0c\u5e76\u629b\u51fa\u4e00\u4e2aInterruptedException<\/code>\u5f02\u5e38\uff0c\u5728\u4ee3\u7801\u4e2dcatch\u8fd9\u4e2a\u5f02\u5e38\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002\u5982\u679c\u7ebf\u7a0b\u4e00\u76f4\u5904\u4e8e\u8fd0\u884c\u72b6\u6001\uff0c\u90a3\u4e48\u53ea\u4f1a\u628a\u8be5\u7ebf\u7a0b\u7684\u4e2d\u65ad\u6807\u5fd7\u8bbe\u7f6e\u4e3a true\uff0c\u4ec5\u6b64\u800c\u5df2\uff0c\u6240\u4ee5interrupt()<\/code>\u5e76\u4e0d\u80fd\u771f\u6b63\u7684\u4e2d\u65ad\u7ebf\u7a0b\uff0c\u4e0d\u8fc7\u5728rpc\u8c03\u7528\u7684\u573a\u666f\u4e2d\uff0c\u8bf7\u6c42\u7ebf\u7a0b\u4e00\u822c\u90fd\u5904\u4e8e\u963b\u585e\u72b6\u6001\uff0c\u7b49\u5f85\u6570\u636e\u8fd4\u56de\uff0c\u8fd9\u65f6interrupt()<\/code>\u65b9\u6cd5\u662f\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a\u7684\u3002<\/p>\n

\u53c2\u8003\uff1aJava\u4e2d\u5982\u4f55\u5b9e\u73b0\u7ebf\u7a0b\u7684\u8d85\u65f6\u4e2d\u65ad<\/p>\n

 <\/p>\n

\u4fee\u6539\u5b50\u7ebf\u7a0b\u7684\u4ee3\u7801\uff1a<\/p>\n

public static void fun_a() { for (; ; ) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(\"\u5b50\u7ebf\u7a0b\u629b\u51fa\u5f02\u5e38\"); \/\/e.printStackTrace(); } System.out.println(\">> \" + DateUtil.getNowTimeString()); } }<\/code><\/pre>\n

\u8fd9\u6b21\u662f\u53ef\u4ee5\u7ed3\u675f\u5b50\u7ebf\u7a0b\uff0c\u524d\u63d0\u662f\u5b50\u7ebf\u7a0b\u81ea\u5df1\u6709\u5f02\u5e38\u6355\u83b7\u673a\u5236\uff0c\u53ef\u4ee5\u63a5\u53d7\u5176\u4ed6\u7ebf\u7a0b\u53d1\u6765\u7684InterruptedException\uff1a<\/code><\/p>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

 <\/p>\n

\u4e3b\u7ebf\u7a0b\u6bcf\u96942\u79d2\u5bf9\u5b50\u7ebf\u7a0b\u8fdb\u884c\u4e00\u6b21Interrupted\uff1a<\/p>\n

package com.t.www; public class Test { final static Object lock = new Object(); volatile boolean stop = false; public static void main(String args[]) throws InterruptedException { Thread thread1 = new Thread() { public void run() { fun_a(); } }; thread1.start(); System.out.println(\"> 1 \u4e3b\u7ebf\u7a0bstart \" + DateUtil.getNowTimeString()); int a = 0; while (a < 3) { Thread.sleep(2000); a++; System.out.println(\"> 2 \u4e3b\u7ebf\u7a0b\u5bf9\u5b50\u7ebf\u7a0b\u5f00\u59cbinterrupt \" + DateUtil.getNowTimeString()); thread1.interrupt(); System.out.println(\"> 3 \u4e3b\u7ebf\u7a0b\u5bf9\u5b50\u7ebf\u7a0b\u5b8c\u6210interrupt \" + DateUtil.getNowTimeString()); } } public static void fun_a() { for (; ; ) { try { System.out.println(\">> 1 \u5b50\u7ebf\u7a0bwait \" + DateUtil.getNowTimeString()); synchronized (lock) { lock.wait(); } Thread.sleep(1000); } catch (InterruptedException e) { \/\/throw new RuntimeException(\"\u5b50\u7ebf\u7a0b\u629b\u51fa\u5f02\u5e38\"); e.printStackTrace(); } System.out.println(\">> 2 \u5b50\u7ebf\u7a0b\u5b8c\u6210 \" + DateUtil.getNowTimeString()); } } } <\/code><\/pre>\n

 \"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

 \u4fee\u6539\u5b50\u7ebf\u7a0b\u4ee3\u7801\uff1a<\/p>\n

try { System.out.println(\">> 1 \u5b50\u7ebf\u7a0bwait \" + DateUtil.getNowTimeString()); synchronized (lock) { lock.wait(); } Thread.sleep(1000); } catch (InterruptedException e) { \/\/throw new RuntimeException(\"\u5b50\u7ebf\u7a0b\u629b\u51fa\u5f02\u5e38\"); \/\/e.printStackTrace(); System.out.println(\">> 2 \u5b50\u7ebf\u7a0b\u6355\u83b7\u5f02\u5e38 \" + DateUtil.getNowTimeString()); }<\/code><\/pre>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

\u4ece\u8fd0\u884c\u7ed3\u679c\u770b\u548c\u524d\u9762\u4e00\u81f4\uff0c\u53ea\u662f\u6ca1\u6709\u629b\u51fa\u5f02\u5e38\u3002 <\/p>\n

 <\/p>\n

\u4fee\u6539\u4ee3\u7801\u5b50\u7ebf\u7a0b\u4f7f\u7528while(!Thread.currentThread().isInterrupted())\u5224\u65ad\uff1a<\/p>\n

public class Test { public static void main(String args[]) throws InterruptedException { Thread thread1 = new Thread() { public void run() { fun_a(); } }; thread1.start(); System.out.println(\"> 1 \u4e3b\u7ebf\u7a0bstart \" + DateUtil.getNowTimeString()); int a = 0; while (a < 3) { Thread.sleep(2000); a++; System.out.println(\"> 2 \u4e3b\u7ebf\u7a0b\u5bf9\u5b50\u7ebf\u7a0b\u5f00\u59cbinterrupt \" + DateUtil.getNowTimeString()); thread1.interrupt(); System.out.println(\"> 3 \u4e3b\u7ebf\u7a0b\u5bf9\u5b50\u7ebf\u7a0b\u5b8c\u6210interrupt \" + DateUtil.getNowTimeString()); } } public static void fun_a() { while(!Thread.currentThread().isInterrupted()){ try { System.out.println(\">> 1 \u5b50\u7ebf\u7a0b \" + DateUtil.getNowTimeString()); Thread.sleep(1000); } catch (InterruptedException e) { \/\/throw new RuntimeException(\"\u5b50\u7ebf\u7a0b\u629b\u51fa\u5f02\u5e38\"); \/\/e.printStackTrace(); System.out.println(\">> 2 \u5b50\u7ebf\u7a0b\u6355\u83b7\u5f02\u5e38 \" + DateUtil.getNowTimeString()); } System.out.println(\">> 3 \u5b50\u7ebf\u7a0b\u5b8c\u6210 \" + DateUtil.getNowTimeString()); } System.out.println(\">> 4 \u5b50\u7ebf\u7a0b\u6b63\u5e38\u7ed3\u675f \" + DateUtil.getNowTimeString()); } } <\/code><\/pre>\n

\u53ef\u4ee5\u770b\u5230\u8fd9\u6b21\u56e0\u4e3a\u5b50\u7ebf\u7a0b\u589e\u52a0\u4e86\u72b6\u6001\u5224\u65ad\u6240\u4ee5\u53ef\u4ee5\u6b63\u5e38\u7ed3\u675f\uff1a <\/p>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

\u6bd4\u8f83\u4f18\u96c5\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf\u5728\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u8981\u4fdd\u8bc1\u53ef\u89c1\u6027\uff1a<\/p>\n

public class Test { private static volatile boolean finished = false; \/\/ \u2460 volatile\u6761\u4ef6\u53d8\u91cf public static void main(String args[]) throws InterruptedException { Thread thread1 = new Thread() { public void run() { fun_a(); } }; thread1.start(); System.out.println(\"> 1 \u4e3b\u7ebf\u7a0bstart \" + DateUtil.getNowTimeString()); int a = 0; while (a < 3) { Thread.sleep(2000); a++; } System.out.println(\"> 1 \u4e3b\u7ebf\u7a0b a=\" +a+\" \"+ DateUtil.getNowTimeString()); finished=true; } public static void fun_a() { while(!finished){ try { System.out.println(\">> 1 \u5b50\u7ebf\u7a0b \" + DateUtil.getNowTimeString()); Thread.sleep(1000); } catch (InterruptedException e) { \/\/throw new RuntimeException(\"\u5b50\u7ebf\u7a0b\u629b\u51fa\u5f02\u5e38\"); \/\/e.printStackTrace(); System.out.println(\">> 2 \u5b50\u7ebf\u7a0b\u6355\u83b7\u5f02\u5e38 \" + DateUtil.getNowTimeString()); } System.out.println(\">> 3 \u5b50\u7ebf\u7a0b\u5b8c\u6210 \" + DateUtil.getNowTimeString()); } System.out.println(\">> 4 \u5b50\u7ebf\u7a0b\u6b63\u5e38\u7ed3\u675f \" + DateUtil.getNowTimeString()); } } <\/code><\/pre>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

------------------<\/p>\n

\u5982\u4f55\u505c\u6b62\u7ebf\u7a0b\u6216\u4efb\u52a1<\/span><\/span><\/h2>\n

 <\/p>\n

\u5982\u4f55\u505c\u6b62\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684java\u7ebf\u7a0b<\/h2>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

\"java\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u5426\u7ed3\u675f\u53e6\u4e00\u4e2a\u6c38\u4e0d\u505c\u6b62\u7684\u7ebf\u7a0b\u6267\u884c_\u5982\u4f55\u7ec8\u6b62\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n

\u4e86\u89e3Java\u4e2d\u7684\u7ebf\u7a0b\u4e2d\u65ad<\/span><\/h2>\n

\u5982\u4f55\u5728Java\u4e2d\u6b63\u786e\u505c\u6b62Thread\uff1f<\/span><\/p>\n

\u4f60\u5982\u4f55\u6740\u6b7bJava\u4e2d\u7684\u7ebf\u7a0b\uff1f<\/span><\/p>\n

\u5982\u4f55\u5728\u8fd0\u884c\u65f6\u505c\u6b62\/\u7ec8\u6b62\u957f\u65f6\u95f4\u8fd0\u884c\u7684Java\u7ebf\u7a0b\uff1f\u8d85\u65f6 - >\u53d6\u6d88 - >\u4e2d\u65ad\u72b6\u6001<\/span><\/p>\n

Java - \u4ece\u4e0d\u540c\u7684\u7c7b\u505c\u6b62\u7ebf\u7a0b<\/span><\/p>\n

How To Stop A Thread In Java?<\/p><\/blockquote>\n