当前位置: 技术问答>java相关
征求正确答案,
来源: 互联网 发布时间:2015-03-13
本文导语: 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 t...
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
大家选那个?并说明为什么????
{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
大家选那个?并说明为什么????
|
我想程序应该是先显示一个0,然后等待5秒中后推出程序
|
You have join the t1 to main
显示100.跟j=100在哪里没关系。new s()的时候j=100就被走到了。
显示100.跟j=100在哪里没关系。new s()的时候j=100就被走到了。
|
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 .
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 .
|
0