精神病医生问题 为啥没反应?
代码如下,参照翁恺老师的实验题目敲出了代码,但不知为啥在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);
}
}