public class TestThread implements Runnable {
public static int X=0;
public static void main(String[] args) {
TestThread t = new TestThread();
Thread th = new Thread(t);
th.start();
Systm.out.println(X);
}
public void run() {
for(int i=0; i<100; i++) {
X += i;
}
}
}-------------------------------------------------------------------------
问:输出结果?
调试多次都输出 0
=====================================
可是总感觉能输出其他的值,如4950之类的虽说main进程优先级很高,可是进程调度都是CPU在弄,总觉得有可能输出其他的, 只是不太容易捕到这种情况! 疑惑!不知有没有可能输出其它数?
public static int X=0;
public static void main(String[] args) {
TestThread t = new TestThread();
Thread th = new Thread(t);
th.start();
Systm.out.println(X);
}
public void run() {
for(int i=0; i<100; i++) {
X += i;
}
}
}-------------------------------------------------------------------------
问:输出结果?
调试多次都输出 0
=====================================
可是总感觉能输出其他的值,如4950之类的虽说main进程优先级很高,可是进程调度都是CPU在弄,总觉得有可能输出其他的, 只是不太容易捕到这种情况! 疑惑!不知有没有可能输出其它数?
(因为调用线程后,再进入run再执行循环语句要好几步呢,而主线程继续执行System....这条语句一步就行了)