为什么此程序死循环?

littlev19 2005-11-14 11:18:25
import java.io.*;
public class Doctor extends Thread
{
private String name;
private InputStream in;
private OutputStream out;
Doctor(String name,InputStream in,OutputStream out)
{
this.name=name;
this.in=in;
this.out=out;
}
public static void main(String [] args)throws IOException
{
PipedInputStream sin1=new PipedInputStream();
PipedOutputStream sout1=new PipedOutputStream(sin1);
PipedInputStream sin2=new PipedInputStream();
PipedOutputStream sout2=new PipedOutputStream(sin2);
Doctor dr1=new Doctor("Wang",sin1,sout2);
Doctor dr2=new Doctor("zhang",sin2,sout1);
dr1.start();
dr2.start();
}
public void run()
{
try{
talk(in,out);
}catch(Exception e){}
}
public void talk(InputStream in,OutputStream out) throws Exception{
{
BufferedReader rd=new BufferedReader(new InputStreamReader(in));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(out),true);
pw.println(name+"hello");
while(true)
{
String question =rd.readLine();
reply(pw,question);
}

}
}
public void reply(PrintWriter out,String question) throws Exception{
{

//Thread.sleep(Math.random()*1000);

out.println(name+":"+question);
}
}
}
...全文
165 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
littlev19 2005-11-20
  • 打赏
  • 举报
回复
该怎么作出判断啊?
VO1D 2005-11-20
  • 打赏
  • 举报
回复
while(true){}; 就是说 while 语句永远为真 为真的话就要循环下去了
shemalu 2005-11-20
  • 打赏
  • 举报
回复
while(true){};就像while(1){};一样~~不死才怪~~
其实出现死循环问题就肯定出在循环语句上嘛~(好象有点废话!)
呵呵~~~~~
zeq258 2005-11-14
  • 打赏
  • 举报
回复
呵呵,,,,
chw8219 2005-11-14
  • 打赏
  • 举报
回复
来晚了,上面说完了.
循环里加个判断啊!
lh8748 2005-11-14
  • 打赏
  • 举报
回复
在你的talk()中:
while(true) //___此处发生永久循环
{
String question =rd.readLine();
reply(pw,question);
}

62,614

社区成员

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

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