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.
答案是什么?以及为什么

解决方案 »

  1.   

    我下面这段测试代码思路不对吗,哪位大虾帮我改正一下,谢谢
    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();
        }
    }
      

  2.   

    c吧,一个线程setx,然后一个线程sety,然后check
      

  3.   

    以前看到个选a的是setx和sety方法都是private的,这个题不知道为什么是a,呵呵