这道题我搞不懂,20.public class SychTest{ private int x; private int
这道题我搞不懂,20.public class SychTest{ private int x; private int y; public void setX(int i){ x=i;} public void setY(int i){y=i;} public Synchronized void setXY(int i){ setX(i); setY(i); } public Synchronized boolean check(){ return =y; } } 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 callled by multiple threads.C.check() can return true when multiple threads call setX and setY separately.D.check() can only return true if SychTest is changed allow x and y to be set separately.
最佳回答
答案是C。好吧,补充一下。这个setXY方法用了synchronized(线程同步),所以会防止多个线程同时访问这个方法。但是setX()和setY()方法没有synchronized保护,而且setX()和setY()是public公开的,因此C提到的情况会出现。A。check() can never return true。(错,因为可以不通过setXY()来更改x和y的值)B。check() can return true when setXY is callled by multiple threads。(错,setXY()是被保护的)C。check() can return true when multiple threads call setX and setY separately。(正确答案。)D。check() can only return true if SychTest is changed allow x and y to be set separately。(错。这个其实原则上是对的。但是这个答案用了only一词(只有在这情况下),所以它错了。因为C也是可以造成x!=y。
最新回答共有2条回答
-
2026-04-01 10:28:04体贴的泥猴桃
回复答案是C。好吧,补充一下。这个setXY方法用了synchronized(线程同步),所以会防止多个线程同时访问这个方法。但是setX()和setY()方法没有synchronized保护,而且setX()和setY()是public公开的,因此C提到的情况会出现。A。check() can never return true。(错,因为可以不通过setXY()来更改x和y的值)B。check() can return true when setXY is callled by multiple threads。(错,setXY()是被保护的)C。check() can return true when multiple threads call setX and setY separately。(正确答案。)D。check() can only return true if SychTest is changed allow x and y to be set separately。(错。这个其实原则上是对的。但是这个答案用了only一词(只有在这情况下),所以它错了。因为C也是可以造成x!=y。
热门文章
- 康达学院专转本五年制
- 高考一个考场分ab卷吗
- not only but also用法
- 某物体做自由落体运动,从释放开始计时,则物体在前2s内的平均速度为______m/s,物体下落2m时的速度大小为______m/s.
- 三角函数公式大全表格
- 地理中考必背知识点2022
- 2013-2014学年小学六年级科学上学期期末考试试卷及答案
- 人教版2014-2015学年小学五年级英语第二学期期中教学质量检测试卷及答案
- 【Linux驱动开发】设备树详解(二)设备树语法详解
- 别跟客户扯细节
- 在别的城市买房子能落户吗
- 卖房前要把装修贷还完吗
- 高中政治教学提高教学效果的方法探究
- “互联网+”背景下的初中英语课堂教学改革与创新策略研究
- 2022年终止合同范本
- 租房合同范本范文
- 如何挑选土豆
- 如何挑选土鸡
