模拟一个线程退出的方法,启动线程,运行一秒后退出线程,这是线程没100毫秒打印一次日志。
class TExit extends Thread{
private boolean loop = true;
int count = 0;
@Override
public void run(){
while (loop){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程在运行"+(++count));
}
}
public void setLoop(boolean loop) {
this.loop = loop;
}
}
public class ThreadExit {
public static void main(String[] args) throws InterruptedException {
TExit tExit = new TExit();
tExit.start();
Thread.sleep(1000);
tExit.setLoop(false);
}
}
原文:https://www.cnblogs.com/ilyar1015/p/14698857.html