线程间数据传输

日期:2017-06-19       浏览:706

线程之间只是简单的等待和唤醒并不能满足我们的需求,如果能让多个线程之间实现数据传输的话就更容易满足多变的需求了。
java中提供了各种各样的输入/输出流stream,是我们能够很方便的对数据进行操作,其中管道流pipeStream是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读取数据。通过使用管道,实现不同线程间的通信,而无需借助于类型临时文件之类的东西。
在java的jdk中提供了4个类来使线程间可以进行通信:
  1. PipedInputStream和PipedOutputStream(用于传输字节流)
  2. PipedReader和PipedWriter(用于传输字符流)
示例代码:
// class WriteData
public class WriteData {

    public void writeMethod(PipedOutputStream out) {
        try{
            System.out.println("write : ");
            for(int i = 0; i < 100; ++ i) {
                String outData = "" + (i + 1);
                out.write(outData.getBytes());
                System.out.print(outData);
            }
            System.out.println();
            out.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
        
    }
}
// class ReadData
public class ReadData {
    
    public void readMethod(PipedInputStream input) {
        try{
            System.out.println("read :");
            byte[] byteArray = new byte[20];
            int readLength = input.read(byteArray);
            while(readLength != -1) {
                String newData = new String(byteArray, 0, readLength);
                System.out.print(newData);
                readLength = input.read(byteArray);
            }
            System.out.println();
            input.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

}
// class ThreadWrite
public class ThreadWrite extends Thread {
    private WriteData write;
    private PipedOutputStream out;
    
    public ThreadWrite(WriteData write, PipedOutputStream out) {
        this.write = write;
        this.out = out;
    }

    @Override
    public void run() {
        write.writeMethod(out);
    }
}
// class ThreadRead
public class ThreadRead extends Thread {
    
    private ReadData read;
    private PipedInputStream input;
    
    public ThreadRead(ReadData read, PipedInputStream input) {
        this.read = read;
        this.input = input;
    }
    
    @Override
    public void run() {
        read.readMethod(input);
    }

}
// class Main
public class Main {
    
    public static void main(String[] args) {
        threadStream();
    }
    
    private static void threadStream() {
        try{
            WriteData wirteData = new WriteData();
            ReadData readData = new ReadData();
            
            PipedInputStream inputStream = new PipedInputStream();
            PipedOutputStream outputStream = new PipedOutputStream();
            
            //inputStream.connect(outputStream);
            outputStream.connect(inputStream);
            
            ThreadRead threadRead = new ThreadRead(readData, inputStream);
            threadRead.start();
            
            Thread.sleep(2000);
            
            ThreadWrite threadWrite = new ThreadWrite(wirteData, outputStream);
            threadWrite.start();
            
            
        } catch(IOException e) {
            e.printStackTrace();
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        
    }

}
使用代码inputStream.connect(outputStream)或outputStream.connect(inputStream)使两个stream之间产生通信链接,这样才可以将数据进行输出和输入。
输出结果:
read :
write : 
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
从程序的打印结果来看,两个线程通过管道流成功实现了数据的传输(字符流同上)。
扫码关注有惊喜

(转载本站文章请注明作者和出处 qbian)

暂无评论

Copyright 2016 qbian. All Rights Reserved.

文章目录