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

轩陌 2013-10-28 04:58:09
最近看到管道这里,自己写了个用管道来实现两个线程通信的程序,但是不知道为什么第一次通信可以成功,写线程可以写入,读线程可以读出,第二次就出现异常,异常信息是Read end dead。看网上很多人讲是因为第二次通信时读线程已经死亡。请问如何能够用管道实现多次通信?
...全文
300 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsyhnxc 2015-02-04
  • 打赏
  • 举报
回复
引用 6 楼 fhw10204130 的回复:
同问,,,也是只能读写一次,就抛出异常了:java.io.IOException:Read end dead.
我擦,陈年老贴,挖坟可耻
zsyhnxc 2015-02-04
  • 打赏
  • 举报
回复
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
class producer extends Thread{
    DataOutputStream d1;
    int i=0;
    Random r=new Random();
    producer(OutputStream out){
        d1=new DataOutputStream(out);
    }
    public void run(){
        
        try{
            while(!Thread.interrupted()){
            	synchronized(d1){
            		if(i<10){
            			double num=r.nextDouble()*10;
                        System.out.println("the num is "+num);
                        d1.writeDouble(num);
                        d1.flush();
                        i++;
                        sleep(500);
            		}else{
            			Thread.yield();
            		}
            	}
            }
        }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{
        	while(!Thread.interrupted()){
        		synchronized(d2){
        			if(d2.available()>0){
                		double num=d2.readDouble();
                        sum+=num;
                        count++;
                        System.out.println("sum is "+sum);
                        sleep(500);
        			}else{
        				Thread.yield();
        			}
        		}
        	}
        }catch(IOException e){
            System.out.println("this is a input IOException");
            e.printStackTrace();
        }catch(InterruptedException e){
        	System.out.println("this is a interruprException");
        }
    }
}
public class Piple{
    public static void main(String[] args){
    	PipedInputStream p1=null;
    	PipedOutputStream p2=null;
    	ExecutorService exec=Executors.newCachedThreadPool();
        try{
            p1=new PipedInputStream();
            p2=new PipedOutputStream(p1);
            producer p=new producer(p2);
            consumer c=new consumer(p1);
            exec.execute(p);
            exec.execute(c);
            Thread.sleep(5000);
            
        }catch(IOException e){
            System.out.println("this is main function Exception 1");
            e.printStackTrace();
        }catch(InterruptedException e){
        	System.out.println("this is main InterruptedException 1");
        	e.printStackTrace();
        }finally{
        	if(p1!=null){
        		try {
					p1.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        	}
        	if(p2!=null){
        		try {
					p2.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        	}
        }
        exec.shutdownNow();
    }
}
改了一下,跑下看看
乔不思 2015-02-04
  • 打赏
  • 举报
回复
引用 8 楼 zsyhnxc 的回复:
[quote=引用 6 楼 fhw10204130 的回复:] 同问,,,也是只能读写一次,就抛出异常了:java.io.IOException:Read end dead.
我擦,陈年老贴,挖坟可耻[/quote]哈哈
xiha00 2015-02-03
  • 打赏
  • 举报
回复
同问,,,也是只能读写一次,就抛出异常了:java.io.IOException:Read end dead.
轩陌 2013-11-08
  • 打赏
  • 举报
回复
刚学会贴代码,我的代码是这样的:

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){
		}
	}
}
轩陌 2013-11-08
  • 打赏
  • 举报
回复
中间consumer的延迟也是500,我改过代码,忘记改这里了,但是修改过以后还是会出现:java.io.IOException: Read end dead 的问题,初学JAVA,麻烦各位帮我一下。
轩陌 2013-11-08
  • 打赏
  • 举报
回复
引用 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){ } } }
失落夏天 2013-10-29
  • 打赏
  • 举报
回复
我就是想知道你是怎么实现的。。 代码。。。 就像我说我的代码总是报异常空指针一样,你知道大体上为什么会出错,但是你肯定不会知道哪里出的错。。
前端_Logic 2013-10-29
  • 打赏
  • 举报
回复
while 能解决吗 不过我发现用管道流在两个线程间来回传递数据,效率非常低,不知道的还以为加了延迟

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧