使用方法
停止
- 定义一个boolean变量
- 定义一个stop方法
休眠
Thread.speed()
礼让
Thread.yield()
插队
Thread.join()
实例
public class ThreadStop implements Runnable{
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("我在写代码。。。。"+i++);
}
}
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
ThreadStop threadStop = new ThreadStop();
Thread t = new Thread(threadStop);
t.start();
for (int i = 0; i < 1000; i++) {
if(i == 900){
threadStop.stop();
System.out.println("我代码写完了");
}
System.out.println("main。。。。"+i);
}
}
}