使用以下代码:
package myapp;
class Runner extends Thread {
public void run(){
for(int i = 0; i < 11; i++){
System.out.println("Hello " + i);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Class1 {
public static void main(String[] args){
Runner t1 = new Runner();
t1.start();
Runner t2 = new Runner();
t2.start();
}
}
我得到以下输出:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
而我的输出应该如下:
Hello 0
Hello 0
Hello 1
Hello 1
Hello 2
Hello 2
Hello 3
Hello 3
Hello 4
Hello 4
Hello 5
Hello 5
Hello 6
Hello 6
Hello 7
Hello 7
Hello 8
Hello 8
Hello 9
Hello 9
Hello 10
Hello 10
怎么了?我正在使用 Eclipse 标准/SDK 版本:Kepler Release
Build id:20130614-0229、jre7u25 和 jdk7u25。