public class ThreadText {
public static void main(String arg[]) {
d1 r = new d1();
r.start();
// Thread td = new Thread(r);
// td.start();
try {
Thread.sleep(10000);
r.flag=false;//10s后终止循环
} catch (InterruptedException e) {
// TODO Auto-g enerated catch block
e.printStackTrace();
}
}
}
class d1 extends Thread{
boolean flag=true;
int i=0;
public void run(){
while(flag){
i++;
System.out.println(i);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}