当前位置: 技术问答>java相关
线程疑问??????
来源: 互联网 发布时间:2017-03-29
本文导语: 1.public class SyncTest ( 2.private int x; 3.private int y; 4.private synchronized void setX (int i) (x=1;) 5.private synchronized void setY (int i) (y=1;) 6.public void setXY(int i)(setX(i); setY(i);) 7.public synchronized Boolean check() (return x !=y;) ...
1.public class SyncTest (
2.private int x;
3.private int y;
4.private synchronized void setX (int i) (x=1;)
5.private synchronized void setY (int i) (y=1;)
6.public void setXY(int i)(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.
这题答案是B ,C为何不对?请释疑
麻烦解释一下c的意思
2.private int x;
3.private int y;
4.private synchronized void setX (int i) (x=1;)
5.private synchronized void setY (int i) (y=1;)
6.public void setXY(int i)(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.
这题答案是B ,C为何不对?请释疑
麻烦解释一下c的意思
|
synchronized
|
synchronized说明这个方法是同步方法,意思是,当你调用这个对象的一个synchronized定义的方法后,就不能调用同一个对象中其他用 synchronized定义的方法了
|
private synchronized void setX (int i) (x=1;)
其中的synchronized有什么作用? 和谁同步?
其中的synchronized有什么作用? 和谁同步?