征求正确答案,
1.class s extends Thread
{int j=0;
public void run() {
try{Thread.sleep(5000);}
catch(Exception e){}
j=100;
}
public static void main(String args[])
{
s t1=new s();
t1.start();
System.out.println(t1.j);
}
}
what you have to do to ensure that 'j' will print 100
you have make t1 as Daemon Thread
You have join the t1 to main
You have to suspend the main when the thread starts and resume it
我认为选2
大家选那个?并说明为什么????
问题点数:80、回复次数:12Top
1 楼liukuncn(爱,还记得么?)回复于 2001-12-31 15:01:39 得分 30
我想程序应该是先显示一个0,然后等待5秒中后推出程序Top
2 楼szdino(阿飞)回复于 2001-12-31 15:05:49 得分 20
You have join the t1 to main
显示100.跟j=100在哪里没关系。new s()的时候j=100就被走到了。Top
3 楼liukuncn(爱,还记得么?)回复于 2001-12-31 15:14:39 得分 0
我觉得当t1.start()后,主线程继续进行,所以先运行System.out.println(t1.j),这时候
j=0,这时主线程让出,开始进行run(),然后线程睡眠5秒,在此其间,没有别的线程中断该线程,所以不会产生异常,所以j不会等于100Top
4 楼chl326(希望你是java高手 拯救我于水火之中)回复于 2001-12-31 15:36:31 得分 0
谁能把这个题详细讲一下?非常感谢!!!Top
5 楼szdino(阿飞)回复于 2001-12-31 15:43:35 得分 0
sorry 我看错了!
应该是0Top
6 楼liukuncn(爱,还记得么?)回复于 2001-12-31 15:45:25 得分 0
在线程休眠期间如果被调用了interrupt方法,则会抛出异常,使j=100,题目中并没有这样的调用,所以不会产生异常,j也就不会等于100。
对了,我还不知道答案是什么呢,我说的对不?Top
7 楼caolyf(小草)回复于 2001-12-31 15:54:45 得分 10
0Top
8 楼hyhong_h(黄黄)回复于 2001-12-31 15:55:42 得分 0
注意花括号!j=100语句不在catch里面。
我也认为答案是2。Top
9 楼frankwoo()回复于 2001-12-31 20:10:09 得分 20
I think the answer should be 3.
coz 1:the two threads here have the same priority, so the main thread has right to run first than the t1.
2.when main thread running first, j is still 100, the only way to let main to give up s suspend or yield.
any further discuss is welcomed .Top
10 楼chl326(希望你是java高手 拯救我于水火之中)回复于 2002-01-04 15:46:59 得分 0
jiezhangTop
11 楼chl326(希望你是java高手 拯救我于水火之中)回复于 2002-01-04 16:07:14 得分 0
jiafenTop
12 楼chl326(希望你是java高手 拯救我于水火之中)回复于 2002-01-04 17:12:11 得分 0
ffTop




