package thread;
public class Test_Sleep extends Thread {
boolean flag = true;
public void run() {
while (flag) {
try {
System.out.println("step 1" );
sleep(50);
if (!this.isAlive()) {
System.out.println("thread is over...");
}else {
System.out.println("step 2" );
System.out.println("step 3" );
System.out.println("step 4" );
System.out.println("step 5" );
System.out.println("step 6" );
System.out.println("step 7" );
System.out.println("step 8" );
System.out.println("step 9" );
System.out.println("step 10" );
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Test_Sleep t = new Test_Sleep();
t.start();
try {
System.out.println("main before sleep ..." );
Thread.sleep(100);
System.out.println("main after sleep ..." );
t.flag=false;
System.out.println("thread is over...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果是:
main before sleep ...
step 1
step 2
step 3
step 4
step 5
step 6
step 7
step 8
step 9
step 10
step 1
main after sleep ...
thread is over... //下面的为什么还能打印出来?????????????
step 2
step 3
step 4
step 5
step 6
step 7
step 8
step 9
step 10