多线程之管道流

没用过,mark,看代码就行,用法逻辑很简单。

java语言中提供了各种各样的流供我们操纵数据,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道读取数据,通过使用管道,实现不同线程间的通信,而无需借助于临时文件之类的东西。

在java jdk中提供了4个类用于线程间通信,分别是字节流和字符流,应用起来大体相同:

1.PipedInputStream和PipedOutputStream

public class WriteData {
    public void writeMethod(PipedWriter out){
        try {
            System.out.println("write:");
            for (int i = 0; i < 100; i++) {
                String data=""+i;
                out.write(data);
                System.out.print(data);
            }
            System.out.println();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class ReadData {
    public void readMethod(PipedReader in){
        try {
            System.out.println("read:");
            char[] byteArray = new char[20];
            int readLength=in.read(byteArray);
            while (readLength!=-1){
                String newData = new String(byteArray, 0, readLength);
                System.out.print(newData);
                readLength=in.read(byteArray);
            }
            System.out.println();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class ThreadWrite extends Thread {
    private WriteData write;
    private PipedWriter out;

    public ThreadWrite(WriteData write, PipedWriter out) {
        this.write = write;
        this.out = out;
    }

    @Override
    public void run() {
        write.writeMethod(out);
    }
}

public class ThreadRead extends Thread{
    private ReadData read;
    private PipedReader in;

    public ThreadRead(ReadData read, PipedReader in) {
        this.read = read;
        this.in = in;
    }

    @Override
    public void run() {
        read.readMethod(in);
    }
}

public class Run {
    public static void main(String[] args) {
        try {
            WriteData writeData = new WriteData();
            ReadData readData = new ReadData();

            PipedWriter pipedWriter = new PipedWriter();
            PipedReader pipedReader = new PipedReader();
            //通过这一行使得输入流和输出流联系起来
            pipedReader.connect(pipedWriter);

            ThreadWrite threadWrite = new ThreadWrite(writeData, pipedWriter);
            threadWrite.start();

            Thread.sleep(2000);

            ThreadRead threadRead = new ThreadRead(readData, pipedReader);
            threadRead.start();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2.PipedReader和PipedWriter

public class ReadData {

    public void readMethod(PipedReader read){
        try {
            System.out.println("read :");
            char[] byteArray=new char[20];
            int readLength=read.read(byteArray);
            while(readLength!=-1){
                String newData=new String(byteArray, 0, readLength);
                System.out.print(newData);
                readLength=read.read(byteArray);
            }
            System.out.println();
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


public class WriteData {

    public void writeMethod(PipedWriter out){
        try {
            System.out.println("write :");
            for(int i=0;i<300;i++){
                String outData=""+(i+1);
                out.write(outData);
                System.out.print(outData);
            }
            System.out.println();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


public class ThreadReader extends Thread {

    ReadData read;
    
    PipedReader input;
    
    public ThreadReader(ReadData read,PipedReader input){
        this.read=read;
        this.input=input;
    }
    
    public void run(){
        read.readMethod(input);
    }
}


public class ThreadWrite extends Thread {

    WriteData write;
    
    PipedWriter out;
    
    public ThreadWrite(WriteData write,PipedWriter out){
        this.write=write;
        this.out=out;
    }
    
    public void run(){
        write.writeMethod(out); 
    }
}


public class Run {

    public static void main(String[] args) {
        try {
            WriteData writeData=new WriteData();
            ReadData readData=new ReadData();
            
            PipedReader inputStream=new PipedReader();
            PipedWriter outputStream=new PipedWriter();
            outputStream.connect(inputStream);
            
            ThreadReader threadRead=new ThreadReader(readData, inputStream);
            threadRead.start();
            
            Thread.sleep(2000);
            
            ThreadWrite threadWrite=new ThreadWrite(writeData, outputStream);
            threadWrite.start();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(并发编程,管道流,多线程)