62,621
社区成员
发帖
与我相关
我的任务
分享
public class project {
public static void main(String[] args) throws Exception {
mythread[] thread = new mythread[10];
for (int i = thread.length-1; i >= 0; i--) {
if (i == thread.length-1) {
thread[i] = new mythread(null);
}else{
thread[i] = new mythread(thread[i + 1]);
}
thread[i].start();
}
Thread.sleep(1000);
thread[0].setAnnounce(false);
}
}
class mythread extends Thread {
public boolean announce;
mythread child = null;
mythread(mythread child) {
this.child = child;//保存下一个线程的引用
announce = true;
}
public void run() {
while (announce) {
System.out.println("===="+this.getName());
}
}
public void setAnnounce(boolean announce){
System.out.println("-------------------------------");
this.announce = announce;
if(child!=null)
child.setAnnounce(announce);
}
}