//本例要求用两个进程实现简单的自动对话,线程与线程间用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编辑过]
----------------解决方案--------------------------------------------------------
//本例要求用两个进程实现简单的自动对话,线程与线程间用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();
System.out.println(question);//加这句你可以看到输出了
reply(pw,question);
}
}
private void reply(PrintWriter out, String question) throws Exception {
Thread.sleep((int)(Math.random()*1000));//这样才能让你的线程睡0-1000毫秒,以前都是睡0毫秒,至于为什么,你自己可以想想
out.println(name+":"+question);
}
}
你的程序一点问题都没有,可是为什么看不到输出呢,因为你没有写输出语句
唯一的一句,是永远也执行不到的.
因为有一个while循环,所以talk方法永远不会返回,直到退出为止
想看到输出只要按红色的地方改一下就可以了
以后写程序要仔细,碰到问题自己可以调试一下
还有,你那个睡眠的代码有问题,我已经帮你改过来了.一定要记得括号的作用域
----------------解决方案--------------------------------------------------------
多谢千里冰封!
今后写程序我会更认真的!!
----------------解决方案--------------------------------------------------------