管道流PipeStream是一种特殊的流,用于在不同线程间直接传送数据,而不需要借助临时文件之类的东西。
jdk中提供了四个类来使线程间可以通信:
1)PipedInputStream和PipedOutputStream
2)PipedReader和PipedWriter
先看看字节流的示例:
1 public class ReadData { 2 3 public void readMethod(PipedInputStream in) throws IOException{ 4 System.out.println("begin read..."); 5 byte[] b = new byte[20]; 6 int readLength = in.read(b); 7 while (readLength != -1) { 8 String newData = new String(b, 0, readLength); 9 System.out.println("read:"+newData); 10 readLength = in.read(b); 11 } 12 in.close(); 13 System.out.println("end read..."); 14 } 15 16 }
1 public class WriteData { 2 3 public void writeMethod(PipedOutputStream out) throws IOException{ 4 System.out.println("begin write..."); 5 for (int i = 0; i < 10; i++) { 6 out.write((""+i).getBytes()); 7 System.out.println("write:"+i); 8 } 9 out.close(); 10 System.out.println("end write..."); 11 } 12 }
1 public class ThreadRead extends Thread { 2 3 private ReadData readData; 4 private PipedInputStream in; 5 6 public ThreadRead(ReadData readData, PipedInputStream in) { 7 this.readData = readData; 8 this.in = in; 9 } 10 11 @Override 12 public void run() { 13 try { 14 readData.readMethod(in); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 } 19 }
1 public class ThreadWrite extends Thread { 2 3 private WriteData writeData; 4 private PipedOutputStream out; 5 6 public ThreadWrite(WriteData writeData, PipedOutputStream out) { 7 this.writeData = writeData; 8 this.out = out; 9 } 10 11 @Override 12 public void run() { 13 try { 14 writeData.writeMethod(out); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 } 19 }
1 public class Run { 2 public static void main(String[] args) throws IOException, InterruptedException { 3 WriteData writeData = new WriteData(); 4 ReadData readData = new ReadData(); 5 6 PipedOutputStream out = new PipedOutputStream(); 7 PipedInputStream in = new PipedInputStream(); 8 9 in.connect(out); 10 11 new ThreadWrite(writeData, out).start(); 12 Thread.sleep(2000); 13 new ThreadRead(readData, in).start(); 14 } 15 }
再看看字符流示例:
1 public class ReadData { 2 3 public void readMethod(PipedReader reader) throws IOException{ 4 System.out.println("begin read..."); 5 char[] c = new char[20]; 6 int readLength = reader.read(c); 7 while (readLength != -1) { 8 String newData = new String(c, 0, readLength); 9 System.out.println("read:"+newData); 10 readLength = reader.read(c); 11 } 12 reader.close(); 13 System.out.println("end read..."); 14 } 15 16 }
1 public class WriteData { 2 3 public void writeMethod(PipedWriter writer) throws IOException{ 4 System.out.println("begin write..."); 5 for (int i = 0; i < 10; i++) { 6 writer.write((""+i)); 7 System.out.println("write:"+i); 8 } 9 writer.close(); 10 System.out.println("end write..."); 11 } 12 }
1 public class ThreadRead extends Thread { 2 3 private ReadData readData; 4 private PipedReader reader; 5 6 public ThreadRead(ReadData readData, PipedReader reader) { 7 this.readData = readData; 8 this.reader = reader; 9 } 10 11 @Override 12 public void run() { 13 try { 14 readData.readMethod(reader); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 } 19 }
1 public class ThreadWrite extends Thread { 2 3 private WriteData writeData; 4 private PipedWriter writer; 5 6 public ThreadWrite(WriteData writeData, PipedWriter writer) { 7 this.writeData = writeData; 8 this.writer = writer; 9 } 10 11 @Override 12 public void run() { 13 try { 14 writeData.writeMethod(writer); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 } 19 }
1 public class Run { 2 public static void main(String[] args) throws IOException, InterruptedException { 3 WriteData writeData = new WriteData(); 4 ReadData readData = new ReadData(); 5 6 PipedWriter writer = new PipedWriter(); 7 PipedReader reader = new PipedReader(); 8 9 writer.connect(reader); 10 11 new ThreadWrite(writeData, writer).start(); 12 Thread.sleep(2000); 13 new ThreadRead(readData, reader).start(); 14 } 15 }