SCJP经典题
1. public class SyncTest {
2. private int x;
3. private int y;
4. public synchronized void setX (int i) (x=1;)
5. public synchronized void setY (int i) (y=1;)
6. public synchronized void setXY(int 1)(setX(i); setY(i);)
7. public synchronized Boolean check() (return x !=y;)
8. }
Under which conditions will check () return true when called from a different class?
A. Check() can never return true.
B. Check() can return true when setXY is called by multiple threads.
C. Check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set
separately.
不同版本答案不一样
应该选什么呀?
问题点数:20、回复次数:3Top
1 楼Paripatetic(逍遥)回复于 2006-03-12 20:37:23 得分 10
默认构造函数将字段初始化为0
所以选cTop
2 楼aGreenApple()回复于 2006-03-12 21:21:09 得分 0
如果第6行是
public void setXY(int 1)(setX(i); setY(i);)
是不是就选B了Top
3 楼ahlon(Ahlon)回复于 2006-03-15 23:44:33 得分 10
选c是对的,lz的第二个想法也是对的。线程不同步会造成x,y值的不一样Top




