当前位置: 技术问答>java相关
又一道scjp题,不懂不懂
来源: 互联网 发布时间:2015-02-27
本文导语: class s implements Runnable{ int x=0,y=0; synchronized void addX(){x++; } synchronized void addY(){y++; } void addXY(){x++;y++;} boolean check() { return (x>y)? true:false;) public void run() { //// System.out.println(check()); } public static void main(String ...
class s implements Runnable{
int x=0,y=0;
synchronized void addX(){x++; }
synchronized void addY(){y++; }
void addXY(){x++;y++;}
boolean check() { return (x>y)? true:false;)
public void run()
{
////
System.out.println(check()); }
public static void main(String args[])
{ s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
If this methods are called in which order the check will return true?
Select all that apply
call addX() and addY() simultaneously for number of times in run()
call addY() and addX() simultaneously for number of times in run()
all addXY() for number of times in run()
Ans:B,C
int x=0,y=0;
synchronized void addX(){x++; }
synchronized void addY(){y++; }
void addXY(){x++;y++;}
boolean check() { return (x>y)? true:false;)
public void run()
{
////
System.out.println(check()); }
public static void main(String args[])
{ s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
If this methods are called in which order the check will return true?
Select all that apply
call addX() and addY() simultaneously for number of times in run()
call addY() and addX() simultaneously for number of times in run()
all addXY() for number of times in run()
Ans:B,C
|
the before thread is no use to do
so call
1.addX()-->XY return true
2.addY()-->XY return false
3.call addXY() for number of times in run()
X will first to be add and before y to be add ... system to run check
X>Y return true
it is just my thought,perhaps it is also wrong 8-)
so call
1.addX()-->XY return true
2.addY()-->XY return false
3.call addXY() for number of times in run()
X will first to be add and before y to be add ... system to run check
X>Y return true
it is just my thought,perhaps it is also wrong 8-)
|
好象C是对的,但别的不明白如何判断