管道流通信


import  java.io.PipedOutputStream;
import  java.io.PipedInputStream;

public   class  Main  {
    
public  Main() {
    }

    
public   static   void  main(String[] args)   throws  Exception {
        PipedOutputStream pos
= new  PipedOutputStream();
        PipedInputStream pis
= new  PipedInputStream();
        pos.connect(pis);
        producer p
= new  producer(pos);
        p.run();
        consumer c
= new  consumer(pis);
        c.run();
    }

    
}


class  producer  extends  Thread {
    
private  PipedOutputStream pos;
    producer(PipedOutputStream pos)
{
        
this .pos = pos;
    }

    
public   void  run() {
        
try {
            pos.write(
" send a message " .getBytes());
            pos.close();
        }
catch (Exception e) {
            e.printStackTrace();
        }

    }

}


class  consumer  extends  Thread {
    
private  PipedInputStream pis;
    consumer(PipedInputStream pis)
{
        
this .pis = pis;
    }

    
public   void  run() {
        
byte  b[] = new   byte [ 100 ];
        
try {
            
int  len = pis.read(b);
            System.out.println(
new  String(b, 0 ,len));
        }
catch (Exception e) {
        e.printStackTrace();
        }

        
    }

}