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);
}
}
}

解决方案 »

  1.   

    在你的talk()中:
    while(true) //___此处发生永久循环
    {
        String question =rd.readLine();
        reply(pw,question);
    }
      

  2.   

    while(true){};就像while(1){};一样~~不死才怪~~
    其实出现死循环问题就肯定出在循环语句上嘛~(好象有点废话!)
    呵呵~~~~~
      

  3.   

    while(true){};  就是说 while 语句永远为真  为真的话就要循环下去了