请问这段java代码为什么没有结果输出呀?

presadio 2005-07-07 12:49:26
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);
}


}
编译能通过,但是没有结果,为什么?
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
congliu 2005-07-07
  • 打赏
  • 举报
回复
while -》if
whmily 2005-07-07
  • 打赏
  • 举报
回复
死循环
楼主看看你的CPU使用率,肯定是100%
改代码如下看看:
public synchronized void write(int n)
{
length=0;
while (this.length<9)
{
line[this.length]=n;
this.length++;
}
}
read方法看你要实现什么功能再改吧
zhumeng459 2005-07-07
  • 打赏
  • 举报
回复
晕死!楼主根本就没有设置跳出While循环的方法。
改称if就跟原代码的目的不同了……
naxin 2005-07-07
  • 打赏
  • 举报
回复
把 while 换成 if 就OK,

死循环。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧