Sorry!写错了。class A extends Thread{
  private B b;
  public A(B b){
    this.b = b;
  }
  public synchronized void run(){
    while(true)
      if(b.getI()!=b.getJ()) System.out.println("E");
  }
}public class B extends Thread{
  private int i = 0; private int j = 0;
  public int getI(){return i;}
  public int getJ(){return j;}
  public synchronized void run(){
    while(true){
      i++; j++;
    }
  }
  public static void main(String[] args){
    B b = new B();
    new A(b).start();
    b.start();
  }
}

解决方案 »

  1.   

    还有这个改正后还是会输出“E”,为什么呢?我晕了。class A extends Thread{
      private B b;
      public A(B b){
        this.b = b;
      }
      public void run(){
        while(true)
          if(b.getI()!=b.getJ()) System.out.println("E");
      }
    }public class B extends Thread{
      private int i = 0; private int j = 0;
      public int getI(){return i;}
      public int getJ(){return j;}
      public void run(){
        while(true){
         synchronized(this){
           i++; j++;
          }
        }
      }
      public static void main(String[] args){
        B b = new B();
        new A(b).start();
        b.start();
      }
    }
      

  2.   

    在调用getI()方法和getJ()方法时有时间先后,不能保证当中i和j的值没有变化,试试这个class A extends Thread{
      private B b;
      public A(B b){
        this.b = b;  }
      public void run(){
          
        while(true)
       
          if(!b.iequalsj())
               System.out.println("E");   }
    }public class B extends Thread{
      private int i = 0; private int j = 0;
      public synchronized boolean iequalsj(){return i==j;};
      public void run(){
        while(true){
            synchronized(this){
            i++; j++;
          }
        }
      }
      public static void main(String[] args){
        B b = new B();
        new A(b).start();
        b.start();
      }
    }
      

  3.   

    你试试这样:   B b = new B();
        Thread thread1=new A(b);
        Thread thread2=new B();
        thread1.start();
        thread2.start();
      

  4.   

    他只做synchronizedb了.原因我也不太明白.
      

  5.   

    synchronized(obj)就是对obj上锁。如果是方法就是对该方法所在的对象(如果是实例方法就是对this)上锁。你可以改成:class A extends Thread{
      private B b;
      public A(B b){
        this.b = b;
      }
      public void run(){
        while(true)
          synchronized(b){
           if(b.getI()!=b.getJ()) System.out.println("E");
          }
      }
    }public class B extends Thread{
      private int i = 0; private int j = 0;
      public int getI(){return i;}
      public int getJ(){return j;}
      public synchronized void run(){
        while(true){
          i++; j++;
        }
      }
      public static void main(String[] args){
        B b = new B();
        new A(b).start();
        b.start();
      }
    }
      

  6.   

    具体你可以看看:http://gceclub.sun.com.cn/NASApp/sme/jive/thread.jsp?forum=8&thread=10970&start=0&msRange=15
      

  7.   

    试试这个吧public static void main(String[] args){
        B b = new B();
        A a=new A(b);
        try{
        a.start();
        a.join();
        b.start();
        b.join();
    }catch(Exception e){
    }
    }