public class SyncTest {
private int x;
private int y;
public synchronized void setX (int i) {x=i;}
public synchronized void setY (int i) {y=i;}
public synchronized void setXY(int i)(set X(i); setY(i);)
public synchronized Boolean check() (return x !=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 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.
答案是什么?以及为什么
private int x;
private int y;
public synchronized void setX (int i) {x=i;}
public synchronized void setY (int i) {y=i;}
public synchronized void setXY(int i)(set X(i); setY(i);)
public synchronized Boolean check() (return x !=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 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.
答案是什么?以及为什么
解决方案 »
- 两个数组比较(Java语言实用)
- Java为何使用 catch Exception e 无法捕获NullPointerException
- 谁来指点我一下呀~~~~
- javac Hello.java时,找不到Hello.java文件
- 达人来看看这个很简单的问题
- java能实现类似qq那种语音聊天程序吗~??
- 请教一个问题?
- exception:com.ms.security.securityException[host]:cannot access "202.113.189.115":1111?
- 我写了异常简单的线程程序,大家看一看错在那里。谢谢
- Java 如何读取一个二进制流中指定长度的数据并转换成整数
- 大家帮帮忙。。一个关于焦点的问题。
- 请各位朋友来看看,帮帮小弟的忙。先表感谢!
public class SychTest{
private int x;
private int y;
public synchronized void setX(int i){ x=i;}
public synchronized void setY(int i){y=i;}
public synchronized void setXY(int i){
setX(i);
setY(i);
}
public synchronized void check(){
System.out.println(x);
System.out.println(y);
System.out.println(x!=y);
}
public static void main(String[] args) {
final SychTest myt2 = new SychTest();
Thread t1 = new Thread(
new Runnable() {
public void run() {
// myt2.setX(1);
while(true)
{myt2.setX(1);}
//myt2.check(); //myt2.check();
}
}
); Thread t2 = new Thread(
new Runnable() {
public void run() {
while(true) {myt2.setY(2);}
//myt2.check();
}
}
); Thread t3 = new Thread(
new Runnable() {
public void run() {
while(true) { myt2.check();}
}
}
);
t1.start();
t2.start();
t3.start();
}
}