class a
{
  int length;
  int line[]=new int[9];
  public synchronized   void write(int n)
   {
   while (this.length<9)
   line[this.length]=n;
   }
  public  synchronized int   read()
  {    int m=0;
    while (this.length>0)
    m=line[this.length];
    return m;
  }
}
  public class aa
  {
public static void main(String args[])
{
 int q;
 a mmm=new a();
 mmm.write(2);
 q=mmm.read();
 System.out.print(q);
 }
 
 
  }
编译能通过,但是没有结果,为什么?

解决方案 »

  1.   

    把 while 换成 if 就OK,死循环。
      

  2.   

    晕死!楼主根本就没有设置跳出While循环的方法。
    改称if就跟原代码的目的不同了……
      

  3.   

    死循环
    楼主看看你的CPU使用率,肯定是100%
    改代码如下看看:
    public synchronized   void write(int n)
       {
        length=0;
       while (this.length<9)
       {
          line[this.length]=n;
          this.length++;
        }
       }
      read方法看你要实现什么功能再改吧