//本例要求用两个进程实现简单的自动对话,线程与线程间用PipedStream进行通信,并将对话过程用System.out输出!!
//下面是我弄的代码,但是修改完后能通过调试但是运行之后没有任何反映,和输出语句.没有提示任何的错误
//麻烦大家帮忙指点一下
import java.io.*;
public class Doctors extends Thread{
private String name;
private InputStream in;
private OutputStream out;
Doctors(String name,InputStream in,OutputStream out) {
this.name = name;
this.in = in;
this.out = out;
}
public static void main(String[] args) throws Exception {
PipedInputStream sin1= new PipedInputStream();
PipedInputStream sin2= new PipedInputStream();
PipedOutputStream sout1= new PipedOutputStream(sin1);
PipedOutputStream sout2= new PipedOutputStream(sin2);
Doctors dr1 = new Doctors("wang",sin1,sout2);
Doctors dr2 = new Doctors("zhang",sin2,sout1);
dr1.start();
dr2.start();
}
public void run(){
try{
talk(in,out);
System.out.println("dsjfksdjf");
} 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 I am Doctor !");
while (true) {
String question = rd.readLine();
reply(pw,question);
}
}
private void reply(PrintWriter out, String question) throws Exception {
Thread.sleep((int)Math.random()*1000);
out.println(name+":"+question);
}
}
[此贴子已经被作者于2006-10-30 15:27:05编辑过]