标签:就会 ted 主线程 一段 rgs 需要 suspend checked 并且
/** * run方法内没有sleep或wait方法时,停止线程 */ public class RightWayStopThreadWithoutSleep implements Runnable{ @Override public void run() { int num = 0; while(num <= Integer.MAX_VALUE / 2){ if (num % 10000 == 0){ System.out.println(num+"是10000的倍数"); } num++; } System.out.println("任务运行结束"); } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new RightWayStopThreadWithoutSleep()); //在主线程中开启子线程,然后主线程休眠1s后通知子线程中断 thread.start(); Thread.sleep(1000); thread.interrupt(); } }
/** * run方法内带有sleep或wait方法时,停止线程 */ public class RightWayStopThreadWithSleep{ public static void main(String[] args) throws InterruptedException { Runnable runnable = ()->{ int num = 0; try { while (num <= 300 && !Thread.currentThread().isInterrupted()){ if (num % 100 == 0){ System.out.println(num + "是100的倍数"); } num++; } Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }; Thread thread = new Thread(runnable); thread.start(); Thread.sleep(500); thread.interrupt(); } }
/** * run方法内每次循环都会带有sleep或wait方法时,停止线程 */ public class RightWayStopThreadWithSleepEveryLoop { public static void main(String[] args) throws InterruptedException { Runnable runnable = ()->{ int num = 0; try { while (num <= 10000){ if (num % 100 == 0){ System.out.println(num + "是100的倍数"); } num++; Thread.sleep(10); } } catch (InterruptedException e) { e.printStackTrace(); } }; Thread thread = new Thread(runnable); thread.start(); Thread.sleep(5000); thread.interrupt(); } }
public class CantInterrupt { public static void main(String[] args) throws InterruptedException { Runnable runnable = () -> { int num = 0; while (num <= 10000) { if (num % 100 == 0) { System.out.println(num + "是100的倍数"); } num++; try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }; Thread thread = new Thread(runnable); thread.start(); Thread.sleep(5000); thread.interrupt(); } }
/** * 最佳实践:catch了InterruptedException之后优先选择:在方法签名中抛出异常 * 最佳做法:必须在run中try/catch来处理异常而不是在run的内部调用方法try/catch, * 并且run方法无法抛出异常 */ public class RightWayStopThreadInProd implements Runnable { @Override public void run() { while (true && !Thread.currentThread().isInterrupted()) { System.out.println("go"); try { throwInMethod(); } catch (InterruptedException e) { System.out.println("保存日志"); e.printStackTrace(); } } } private void throwInMethod() throws InterruptedException { Thread.sleep(2000); } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new RightWayStopThreadInProd()); thread.start(); Thread.sleep(1000); thread.interrupt(); } }
/** * 最佳实践:被调用的方法中在catch语句中调用Thread.currentThread().interrupt来恢复中断 */ public class RightWayStopThreadInProd2 implements Runnable { @Override public void run() { while (true) { if (!Thread.currentThread().isInterrupted()){ System.out.println("Interrupted,程序运行结束"); break; } throwInMethod(); } } private void throwInMethod() { try { Thread.sleep(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new RightWayStopThreadInProd2()); thread.start(); Thread.sleep(1000); thread.interrupt(); } }
标签:就会 ted 主线程 一段 rgs 需要 suspend checked 并且
原文地址:https://www.cnblogs.com/zhihaospace/p/12509454.html