PipedInputStream和PipedOutputStream分別是管道輸入流和管道輸出流.java
它們的做用是讓多線程之間能夠經過管道進行通信,在使用管道通訊時,必須將PipedInputStream和PipedOutputStream配合使用.數組
使用管道通訊時,大體流程是:線程A向PipedOutputStream中寫入數據,這些數據會自動的發送到對應的pipedInputStream中進行緩存,此時,線程B經過讀取PipedInputStream中的數據,就能夠實現線程通訊了.緩存
實驗一:發送簡短的消息多線程
Sender.java(發送消息)app
public class Sender extends Thread { private PipedOutputStream pos = new PipedOutputStream(); public Sender(PipedOutputStream pos) { this.pos = pos; } @Override public void run() { sendShortMessage(); } // 發送簡單的消息 public void sendShortMessage() { try { pos.write("你好啊!".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { pos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Reciver.java(接受消息)ide
public class Reciver extends Thread { private PipedInputStream pis = new PipedInputStream(); public Reciver(PipedInputStream pis) { this.pis = pis; } @Override public void run() { byte[] buf = new byte[2048]; try { pis.read(buf); pis.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Reciver :" + new String(buf)); } }
PipedTest.javaui
public class PipedTest { public static void main(String[] args) throws IOException { PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(); Sender sender = new Sender(pipedOutputStream); Reciver reciver = new Reciver(pipedInputStream); pipedInputStream.connect(pipedOutputStream); sender.start(); reciver.start(); } } //Reciver :你好啊!
實驗一很好理解:this
pipedInputStream.connect(pipedOutputStream);方法把PipedOutPutStream和PipedInputStream關聯了起來.
Sender經過write()方法,向PipedInputStream中寫入"你好啊!",查看PipedOutputStream的writer()方法可知,實際上是調用了PipedinputStream的receive()方法,繼續查看該方法可知,PipedinputStream的
receive()方法,其實就是把數據保存到本身的byte buffer[]數組中,並且其數組的默認大小爲1024.
Reciver經過read()方法,從本身的byte buffer[]數組中讀取數據.
實驗二:發送較長的消息spa
Sender.java(發送消息)線程
public class Sender extends Thread { private PipedOutputStream pos = new PipedOutputStream(); public Sender(PipedOutputStream pos) { this.pos = pos; } @Override public void run() { sendShortMessage(); } // 發送較長的消息 public void sendLongMessage() { StringBuilder sb = new StringBuilder(); // 總共長度是1020個字節 for (int i = 0; i < 102; i++) { sb.append("0123456789"); } // 再寫入26個字節。 sb.append("abcdefghijklmnopqrstuvwxyz"); // str的總長度是1020+26=1046個字節 String str = sb.toString(); try { pos.write(str.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { pos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Reciver.java(接受消息)
public class Reciver extends Thread { private PipedInputStream pis = new PipedInputStream(); public Reciver(PipedInputStream pis) { this.pis = pis; } @Override public void run() { byte[] buf = new byte[2048]; try { pis.read(buf); pis.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Reciver :" + new String(buf)); } }
PipedTest.java
public class PipedTest { public static void main(String[] args) throws IOException { PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(); Sender sender = new Sender(pipedOutputStream); Reciver reciver = new Reciver(pipedInputStream); pipedInputStream.connect(pipedOutputStream); sender.start(); reciver.start(); } }
實驗二的運行結果爲:Reciver :012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789abcd
發現收到的數據少了"efghijklmnopqrstuvwxyz",爲何打印出來的數據正好是1024長度.其實上面咱們已經說過了,PipedInputStream的byte [] buffer數組的默認長度只有1024,因此上面的輸出是這樣的.那怎麼改才能輸出所有內容的?
咱們修改Reciver.java
public class Reciver extends Thread { private PipedInputStream pis = new PipedInputStream(); public Reciver(PipedInputStream pis) { this.pis = pis; } @Override public void run() { reciverLongMessage(); } public void reciverLongMessage() { while (true) { byte[] buf = new byte[1024]; int len; try { len = pis.read(buf); if (len > 0) { System.out.println(new String(buf, 0, len)); } else { break; } } catch (IOException e) { try { pis.close(); } catch (IOException e2) { e2.printStackTrace(); } } } } }
這樣就能夠正確輸出結果啦!