Thread+isInterrupted+自定义(停止线程)
作者:互联网
1.interrupt终端线程
public static void main(String[] args) throws InterruptedException { //创建子线程 Thread thread1=new Thread(()->{ while (!Thread.currentThread().isInterrupted()){ System.out.println("子线程即将进入休眠阶段"); try { Thread.sleep(1000); //处理业务 System.out.println("子线程运行中"); } catch (InterruptedException e) { //e.printStackTrace();//打印异常信息 System.out.println("thread1线程接到终止命令"); break; } } }); thread1.start();//启动子线程 Thread.sleep(5000);//主线程休眠5秒,为子线程腾出5秒的运行时间 thread1.interrupt();//子线程停止 }
interrupt方式是java自带的线程停止方式。
搜索
复制
标签:自定义,Thread,System,isInterrupted,线程,thread1,interrupt,out 来源: https://www.cnblogs.com/lianggegege123/p/16622886.html