今天在使用线程创建Thread对象使用中调用stop方法IEDA给出提示
Warning:(13, 12) java: java.lang.Thread中的stop()已过时
查看帮助文档后看到官方解释stop这种方法本质上是不安全的,所以这里对于需要停止线程的方法我使用interrupt这个方法,可以达到中断线程的效果
/**
* @author Cartel
* @version 1.0
* @date 2021/4/16 11:13
*/
public class Test {
public static void main(String[] args) {
//创建线程对象
Thread run = new Thread();
//开启线程
run.start();
System.out.println("Start");
//停止线程
run.stop();
//创建线程对象
Thread runSecond = new Thread();
//开启线程
runSecond.start();
System.out.println("Start again");
//中断这个线程
runSecond.interrupt();
}
}