public class ThreadTest终止线程执行 {
public static void main(String[] args) {
StopThread st = new StopThread();
Thread t = new Thread(st);
t.setName("t");
t.start();
//让main睡5秒
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//睡完直接结束
st.r = false;
}
}
class StopThread implements Runnable{
boolean r = true;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if(r){
System.out.println(Thread.currentThread().getName()+"----"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
return;
}
}
}
}
多线程的线程终止
最新推荐文章于 2022-08-03 16:40:11 发布