这道题我搞不懂,20.public class SychTest{ private int x; private int
来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:英语作业 时间:2024/05/14 15:04:03
这道题我搞不懂,
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.
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.
好吧,补充一下.
这个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.
这道题我搞不懂,20.public class SychTest{ private int x; private int
#include class base { Private:int x,y; public:void setxy(int
自定义包时出现的问题package com.juj;public class Point{ private int x,
1.给出如下代码:class Test{ private int m; public static void fun()
class Point{ protected int x,y; public Point(){} public Poin
class A{int i,j;public:static int x;public:A(int a = 0,int b
已知有如下类的定义:public class Point { private double x,y; public Po
public private和private区别?
搞不懂 求解释~~class A{ int x=0,y=0; A(int x,int y){ this.x=x;this
[code=C#] public class Instance { private class SingletonIns
public class test(){ public Class a(int i) {} public static
class Point{ private double x; private double y; Point(doubl