代码拉取完成,页面将自动刷新
public class Demo9 extends Thread {
boolean flag = true;
public Demo9(String name){
super(name);
}
@Override
public synchronized void run() {
int i = 0;
while(flag){
try {
this.wait(); // 狗娃等待...
} catch (InterruptedException e) {
System.out.println("接收到了一个InterruptedException.."); //狗娃
}
System.out.println(Thread.currentThread().getName()+":"+ i);
i++;
}
}
public static void main(String[] args) {
//创建线程对象
Demo9 d = new Demo9("狗娃");
d.start();
//当主线程的i到80的时候,停止狗娃线程。
for(int i = 0 ; i<100 ; i++){
if(i==80){
// d.flag = false; //interrupt() 无法停止一个线程,
d.interrupt(); //强制清除一个线程的wait、 sleep状态。 可以指定清除哪个线程。
}
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。