class A implements Runnable{
public int i=1;
public void run(){
this.i=10;
}
}
public class Test{
public static void main(String args){
A a=new A();
11) new Thread(a).start();
int j=a.i;
13)
}
}
what is the value of j at line 13?
A. 1
B. 10
C. the value of j cannot be determined
D. An error at line 11 cause compilation to fail
Answer:C 问题1 我测试下来应该是1,为什么选C,是不是答案有问题2.这里this.i这句是否有意义,因为这里的run方法比较特殊
public int i=1;
public void run(){
this.i=10;
}
}
public class Test{
public static void main(String args){
A a=new A();
11) new Thread(a).start();
int j=a.i;
13)
}
}
what is the value of j at line 13?
A. 1
B. 10
C. the value of j cannot be determined
D. An error at line 11 cause compilation to fail
Answer:C 问题1 我测试下来应该是1,为什么选C,是不是答案有问题2.这里this.i这句是否有意义,因为这里的run方法比较特殊
new Thread(a).start();
Thread.sleep(1);
int j = a.i;
System.out.println(j);
这个牵涉到thread,因为程序是无法决定在某一时刻会执行那个thread。在new Thread(a).start();执行后,其实至少存在了两条线程,main和new Thread(a),接下来时间片会选那个运行只能由操作系统决定。你测试的是一个例,别被表象迷惑了