這裏的輸出是SCJP轉儲一個問題:掌握兩個線程執行
public class Threads1 {
int x=0;
public class Runner implements Runnable{
public void run(){
int current=0;
for (int i=0; i<4; i++){
current = x;
System.out.print(current + ',');
x=current +2;
}
}
}
public static void main(String[] args){
new Threads1().go();
}
public void go(){
Runnable r1 = new Runner();
new Thread(r1).start();
new Thread(r1).start();
}
}
可能是什麼結果?
A. 0,2,4,4,6,8,10,6,
B. 0,2,4,6,8,10,2,4,
C. 0,2,4,6,8,10,12,14,
D. 0,0,2,4,4,6,6,8,8,10,10,12,12, 14,14,
E. 0,2,4,6,8,10,12,14,0,2,4,6,8,10,12,14,
在轉儲它說答案是A和C.但是,我不知道h由於最後一個輸出(6)比之前的輸出小,所以可以回答A。