精神病医生问题 为啥没反应?

magicrafer 2009-10-31 01:45:29
代码如下,参照翁恺老师的实验题目敲出了代码,但不知为啥在Eclipse上运行时确没有结果。
请达人帮忙解答!

public class Doctor extends Thread{

private String name;
private InputStream in;
private OutputStream out;

public Doctor(String name,InputStream in,OutputStream out) {
// TODO Auto-generated constructor stub
this.name=name;
this.in=in;
this.out=out;
}

public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
PipedInputStream pis1=new PipedInputStream();
PipedOutputStream pos1=new PipedOutputStream(pis1);
PipedInputStream pis2=new PipedInputStream();
PipedOutputStream pos2=new PipedOutputStream(pis2);

Doctor dr1=new Doctor("Joey",pis1,pos2);
Doctor dr2=new Doctor("Hanna",pis2,pos1);
dr1.start();
dr2.start();
//dr.talk(System.in,System.out);
}
public void run(){
try{
talk(in, out);
}catch (Exception e){

}
}
void talk(InputStream in,OutputStream out) throws Exception{
BufferedReader bis=new BufferedReader(new InputStreamReader(in));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(out),true);
while (true){
String question= bis.readLine();
reply(pw,question);
}
}
void reply(PrintWriter out,String str) throws Exception{
Thread.sleep(1000);
out.println(name+str);
}
}
...全文
75 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

58,441

社区成员

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

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