综合问题~急~~

zeroman20 2005-02-25 12:35:26
编译时候没有问题,运行时候 停在run()不动了 请大家帮我看看
import java.io.*;

public class ThreadDoctors implements Runnable
{
private String name;
private PipedInputStream sin;
private PipedOutputStream sout;
ThreadDoctors(String name,PipedInputStream sin,PipedOutputStream sout)
{
this.name = name;
this.sin = sin;
this.sout = sout;
}
public void talks(InputStream in,OutputStream out) throws IOException
{
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out),true);
pw.println("Hello!");
while(true)
{
String question = rd.readLine();
reply(pw,question);
}
}
private void reply(PrintWriter pw ,String question) throws IOException
{
pw.println(name+":"+question);
}
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);
ThreadDoctors dr1 = new ThreadDoctors("wang",sin1,sout2);
ThreadDoctors dr2 = new ThreadDoctors("zhang",sin2,sout1);
Thread t1 = new Thread(dr1);
Thread t2 = new Thread(dr2);
//dr.talks(System.in,System.out);
t1.start();
t2.start();
}
public void run()
{
try
{
talks(sin,sout);
}
catch(IOException e){};
}
}
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeroman20 2005-03-01
  • 打赏
  • 举报
回复
哥哥 还是不行~
我在run()里 加了一条System.out.println("This");语句测试
public void run()
{
System.out.println("This");
try
{
talks(sin,sout);
}
catch(IOException e){};
}
发现输出两个This ,可是接下来就停了!
zjh527 2005-02-28
  • 打赏
  • 举报
回复
缺少跳出条件

while((String question = rd.readLine())!=null)
{
reply(pw,question);
}
zeroman20 2005-02-28
  • 打赏
  • 举报
回复
好象不行 哥哥们帮我看看
crow_soup 2005-02-25
  • 打赏
  • 举报
回复
sorry, 上面我搞错了,

原因就是 while 循环没有地方退出,

String question;
while(null != (question = rd.readLine())){
reply(pw,question);
}

试试看

crow_soup 2005-02-25
  • 打赏
  • 举报
回复
这真是个漂亮的死循环啊,
while(true){} 还没有跳出的地方,

另外,既然是 PipedInputStream ,你确实是调了 xxx | java ThreadDoctors 的吧

62,614

社区成员

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

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