当前位置: 技术问答>java相关
请问这道题答案是什么呢?
来源: 互联网 发布时间:2015-05-14
本文导语: Given: 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)(set X(i); setY(i);) 7. public synchronized Boolean...
Given:
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)(set X(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 N/A
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.
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)(set X(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 N/A
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.
|
这个程序会死锁吧,setXY占用了当前的lock,然后setX调用会寻找lock 才能能运行,但是setXY不结束,setX永远也得不到lock
说错了,别怪我
说错了,别怪我