一个SCJP试题,为什么答案是1,而不是10??? 你没有调用run()方法,只是将ABC实例化,程序运行过程中并未改变10赋值给i,所以还是把1返回! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 线程并没有马上将10赋给i请看class ABC implements Runnable{ public int i=1; public void run() { this.i=10; }} class Dominic{ public static void main(String args[]) { ABC a=new ABC(); new Thread(a).start(); while(a.i==1) ; System.out.println(a.i); }}输出为10 new Thread(a).start();可能是这步上导致的你的线程没有启动试试这样:public class Dominic{ public static void main(String args[]) { ABC a=new ABC(); Thread mythread=new Thread(a); mythread.start(); int j=a.i; System.out.println(j); }} 这是因为在new Thread(a).start();之后,并没有马上开始这个线程,而是继续在执行主线程。如果加上join(),则可使输出为10:class ABC implements Runnable{ public int i=1; public void run() { this.i=10; }}public class Dominic{ public static void main(String args[]) { ABC a=new ABC(); Thread t = new Thread(a); t.start(); try{ t.join(); } catch(Exception e) { e.printStackTrace(); } int j=a.i; System.out.println(j); }} to ispring:不行,程序进入死循环,意味着没有a.i=100出现。 我认为是线程的启动时间的原因,当我在JB里用调试模式时,因为是step over所以执行到j=a.i时j已经变为10了.当然最后的输出也是10 to ispring:我在1.2上编译运行了十分钟,还没有出结果。to namowen:可以。 这是个考试选择题,A 1B 10C the value of j cannot be determinedD An error cause compilation to fail到底选哪个呢??? 程序运行到start()处 便继续执行 等于有两个可运行态的线程我觉得j应该不确定 ispring 的不行,lanyunfeng的可以,核心思想就是让出当前main线程的时间片。...ABC a=new ABC();new Thread(a).start();Thread.yield();int j=a.i;System.out.println(j);...好像也可以的吧,但lanyunfeng 的好一些,保证得到10 to lanyunfengABC a=new ABC();Thread mythread=new Thread(a);mythread.start();和ABC a=new ABC(); new Thread(a).start();有什么区别? 一个线程初始化的时间比int j=a.i;System.out.println(j);这样的代码耗费时间要长得多!其实这里现成还没运行到int j=a.i;这句,你让主线程SLEEP几个毫秒看看,结果就会不一样! SJCP的题目就是TMD烂!这个题我觉得是模棱两可,如果从线程的耗费时间上来考虑的话,就脱离了Java的初衷了。别忘了,这个还是跟你的操作系统有关的。 这是个考试选择题,A 1B 10C the value of j cannot be determinedD An error cause compilation to fail到底选哪个呢???选 C 把你的EMILtell me.qq117637 如何生成jar文件?谢谢 谁能帮我解释一下这个正则表达式吗?有点看不懂! 新手菜鸟报道~奋斗从此开始! Java 的 showConfirmDialog 的按钮之间不能用方向键切换吗? 谢谢大家推荐一些好的J2SE习题 远程下载的乱码问题 怎么将类似\u5173\u4e8e的东西转化为汉字? j2sdk 1.3教学软件 关与类中函数调用问题 菜鸟初学java,关于移位操作老看不明白,请高手帮忙解释一下 请问JAVA在读库时,用state或PreparedStatement得到记录集的时候,总会出现不响应的停顿,请问这是怎么回事?如何解决?分不够再加谢谢!!! 请教参数传递问题?
请看
class ABC implements Runnable
{
public int i=1;
public void run()
{
this.i=10;
}
} class Dominic
{
public static void main(String args[])
{
ABC a=new ABC();
new Thread(a).start();
while(a.i==1)
; System.out.println(a.i);
}
}
输出为10
可能是这步上导致的你的线程没有启动
试试这样:
public class Dominic
{
public static void main(String args[])
{
ABC a=new ABC();
Thread mythread=new Thread(a);
mythread.start();
int j=a.i;
System.out.println(j);
}
}
class ABC implements Runnable
{
public int i=1;
public void run()
{
this.i=10;
}
}public class Dominic
{
public static void main(String args[])
{
ABC a=new ABC();
Thread t = new Thread(a);
t.start();
try{
t.join();
}
catch(Exception e) {
e.printStackTrace();
}
int j=a.i;
System.out.println(j);
}
}
不行,程序进入死循环,意味着没有a.i=100出现。
我在1.2上编译运行了十分钟,还没有出结果。to namowen:
可以。
A 1
B 10
C the value of j cannot be determined
D An error cause compilation to fail到底选哪个呢???
我觉得j应该不确定
...
ABC a=new ABC();
new Thread(a).start();
Thread.yield();
int j=a.i;
System.out.println(j);
...
好像也可以的吧,但lanyunfeng 的好一些,保证得到10
ABC a=new ABC();
Thread mythread=new Thread(a);
mythread.start();
和
ABC a=new ABC();
new Thread(a).start();
有什么区别?
耗费时间要长得多!
其实这里现成还没运行到int j=a.i;这句,你让主线程SLEEP几个毫秒看看,结果就会不一样!
A 1
B 10
C the value of j cannot be determined
D An error cause compilation to fail到底选哪个呢???选 C
qq117637