当前位置:编程学习 > JAVA >>

如何用管道实现线程间多次通信,不是一次

最近看到管道这里,自己写了个用管道来实现两个线程通信的程序,但是不知道为什么第一次通信可以成功,写线程可以写入,读线程可以读出,第二次就出现异常,异常信息是Read end dead。看网上很多人讲是因为第二次通信时读线程已经死亡。请问如何能够用管道实现多次通信? 通信 线程 异常 --------------------编程问答-------------------- while 能解决吗

不过我发现用管道流在两个线程间来回传递数据,效率非常低,不知道的还以为加了延迟 --------------------编程问答-------------------- 我就是想知道你是怎么实现的。。
代码。。。

就像我说我的代码总是报异常空指针一样,你知道大体上为什么会出错,但是你肯定不会知道哪里出的错。。 --------------------编程问答--------------------
引用 2 楼 AA5279AA 的回复:
我就是想知道你是怎么实现的。。
代码。。。

就像我说我的代码总是报异常空指针一样,你知道大体上为什么会出错,但是你肯定不会知道哪里出的错。。

不好意思,不太熟悉CSDN的规则,我的代码如下:

import java.io.*;
import java.util.Random;

class producer extends Thread{
DataOutputStream d1;
int i=0;
Random r=new Random();
producer(OutputStream out){
d1=new DataOutputStream(out);
}
public void run(){
//System.out.println("producer");

try{
while(i<10){
double num=r.nextDouble()*10;
System.out.println("the num is "+num);
d1.writeDouble(num);
d1.flush();
i++;
sleep(500);
}
}catch(IOException e){
System.out.println("this is a output IOExecption");
e.printStackTrace();
}catch(InterruptedException e){
System.out.println("this is a interruprException");
//e.printStackTrace();
}
}
}
class consumer extends Thread{
DataInputStream d2;
int count=0;
double sum=0;
consumer(InputStream in){
d2=new DataInputStream(in);
}
public void run(){
try{
double num=d2.readDouble();
sum+=num;
count++;
System.out.println("sum is "+sum);
sleep(1000);
}catch(IOException e){
System.out.println("this is a input IOException");
e.printStackTrace();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public class Piple{
public static void main(String[] args){
try{
PipedInputStream p1=new PipedInputStream();
PipedOutputStream p2=new PipedOutputStream(p1);
producer p=new producer(p2);
consumer c=new consumer(p1);
p.start();
c.start();
Thread.sleep(5000);
p.stop();
c.stop();
}catch(IOException e){
System.out.println("this is main function Exception 1");
e.printStackTrace();
}catch(InterruptedException e){
}
}
} --------------------编程问答-------------------- 中间consumer的延迟也是500,我改过代码,忘记改这里了,但是修改过以后还是会出现:java.io.IOException: Read end dead 的问题,初学JAVA,麻烦各位帮我一下。 --------------------编程问答-------------------- 刚学会贴代码,我的代码是这样的:

import java.io.*;
import java.util.Random;

class producer extends Thread{
DataOutputStream d1;
int i=0;
Random r=new Random();
producer(OutputStream out){
d1=new DataOutputStream(out);
}
public void run(){
//System.out.println("producer");

try{
while(i<10){
double num=r.nextDouble()*10;
System.out.println("the num is "+num);
d1.writeDouble(num);
d1.flush();
i++;
sleep(500);
}
}catch(IOException e){
System.out.println("this is a output IOExecption");
e.printStackTrace();
}catch(InterruptedException e){
System.out.println("this is a interruprException");
//e.printStackTrace();
}
}
}
class consumer extends Thread{
DataInputStream d2;
int count=0;
double sum=0;
consumer(InputStream in){
d2=new DataInputStream(in);
}
public void run(){
try{
double num=d2.readDouble();
sum+=num;
count++;
System.out.println("sum is "+sum);
sleep(500);
}catch(IOException e){
System.out.println("this is a input IOException");
e.printStackTrace();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public class Piple{
public static void main(String[] args){
try{
PipedInputStream p1=new PipedInputStream();
PipedOutputStream p2=new PipedOutputStream(p1);
producer p=new producer(p2);
consumer c=new consumer(p1);
p.start();
c.start();
Thread.sleep(5000);
p.stop();
c.stop();
}catch(IOException e){
System.out.println("this is main function Exception 1");
e.printStackTrace();
}catch(InterruptedException e){
}
}
}
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,