詳解如何在code block建立一個C語言的專案
阿新 • • 發佈:2020-12-20
jdk中,Thread類的stop方法不提供使用,那麼如何優雅的停止執行緒?
看程式碼:
public class MyThread14 implements Runnable{
private boolean flag=true;
@Override
public void run() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (flag) {
System.out.println("hello,你好"+i);
}else {
break ;
}
}
}
public void stop() {
this.flag = false;
System.out.println("我給你優雅的停止了執行緒");
}
public static void main(String[] args) throws InterruptedException {
MyThread14 myThread14 = new MyThread14();
System.out.println("啟動");
new Thread(myThread14).start();
Thread.sleep(100);
myThread14.stop();//呼叫自己的stop方法停止執行緒
}
}