public class ThreadTest extends Thread {
private String s;
public ThreadTest(String str){
s=str;
} public void run(){
for(int i=0;i<10; i++){
System.out.println(s +":"+i);
try{
Thread.sleep(1000);
}catch (Exception e){}
}
}public static void main(String[] arg){
ThreadTest mt= new ThreadTest("Thread");
for(int i=0;i<=4;i++)
new Thread(mt).start();
System.out.println("This is ThreadTest");
}
} 输出结果是
Thread:0
This is ThreadTest
Thread:0
Thread:0
Thread:0
Thread:0
Thread:1
Thread:1
Thread:1
Thread:1
Thread:1
Thread:2
Thread:2
Thread:2
Thread:2
Thread:2
Thread:3
Thread:3
Thread:3
Thread:3
Thread:3
Thread:4
Thread:4
Thread:4
Thread:4
Thread:4
Thread:5
Thread:5
Thread:5
Thread:5
Thread:5
Thread:6
Thread:6
Thread:6
Thread:6
Thread:6
Thread:7
Thread:7
Thread:7
Thread:7
Thread:7
Thread:8
Thread:8
Thread:8
Thread:8
Thread:8
Thread:9
Thread:9
Thread:9
Thread:9
Thread:9
更改两个循环变量的时候This is ThreadTest总是出现在不同位置,我觉得应该都在最后输出,请假下为什么呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货