這篇文章主要為大家詳細介紹了java多線程編程之線程間的通信,探討使用管道進行通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下
上一章節講了wait/notify通信,這一節我們來探討使用管道進行通信。
java中提供了IO流使我們很方便的對資料進行操作,pipeStream是一種特殊的流,用於不同線程間直接傳送資料。一個執行緒將資料傳送到輸出管道,另一個執行緒從輸入管道讀取資料。透過管道實現通訊不需要藉助臨時文件這類東西。
java中提供了四個類別使得線程間可以通訊:
①位元組流:PipeInputStream,PipedOutputStream
②字元流:PipedReader,PipedWriter
下面我們來看看位元組流的實作方法:
package pipeInputOutput; //输出流 import java.io.IOException; import java.io.PipedOutputStream; public class WriteDate { public void writeMethod(PipedOutputStream out) { try { System.out.println("write:"); for(int i=0;i<300;i++) { String outDate=""+(i+1); out.write(outDate.getBytes()); System.out.print(outDate); } System.out.println(); out.close(); }catch(IOException e) { e.printStackTrace(); } } }
package pipeInputOutput; //输入流 import java.io.IOException; import java.io.PipedInputStream; public class ReadDate { public void ReadDate(PipedInputStream input) { try { System.out.println("read:"); byte[] byteArray=new byte[20]; int readLength=input.read(byteArray); while(readLength!=-1) { String newDate=new String(byteArray,0,readLength); System.out.print(newDate); readLength=input.read(byteArray); } System.out.println(); input.close(); }catch(IOException e){ e.printStackTrace(); } } }
package pipeInputOutput; import java.io.PipedOutputStream; //输出线程 public class ThreadWrite extends Thread { private WriteDate write; private PipedOutputStream out; public ThreadWrite(WriteDate write,PipedOutputStream out) { super(); this.write=write; this.out=out; } public void run() { write.writeMethod(out); } }
package pipeInputOutput; import java.io.PipedInputStream; //输入线程 public class ThreadRead extends Thread{ private ReadDate read; private PipedInputStream in; public ThreadRead(ReadDate read,PipedInputStream in) { super(); this.read=read; this.in=in; } public void run() { read.ReadDate(in); } }
package pipeInputOutput; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; //测试方法 public class Run { public static void main(String[] args) { try { WriteDate write=new WriteDate(); ReadDate read=new ReadDate(); PipedInputStream inputStream=new PipedInputStream(); PipedOutputStream outputStream=new PipedOutputStream(); //输出流与输入流进行连接。 outputStream.connect(inputStream); //inputStream.connect(outputStream); ThreadRead readThread=new ThreadRead(read,inputStream); readThread.start();//先启动输出线程 Thread.sleep(2000); ThreadWrite writeThread=new ThreadWrite(write,outputStream); writeThread.start();//后启动输入线程 } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
控制台輸出:
write:
123456789101112131415161718192021...
123456789101112131415161718192021...
package pipeInputOutput1; import java.io.IOException; import java.io.PipedWriter; //字符输出流 public class WriteDate { public void writeMethod(PipedWriter out) { try { System.out.println("write:"); for(int i=0;i<300;i++) { String outDate=""+(i+1); out.write(outDate); System.out.print(outDate); } System.out.println(); out.close(); }catch(IOException e) { e.printStackTrace(); } } }
package pipeInputOutput1; import java.io.IOException; import java.io.PipedReader; //字符输入流 public class ReadDate { public void readMethod(PipedReader in) { try { System.out.println("read:"); char[] byteArray=new char[20]; int readLength=in.read(byteArray); while(readLength!=-1) { String newDate=new String(byteArray,0,readLength); System.out.print(newDate); readLength=in.read(byteArray); } System.out.println(); in.close(); } catch (IOException e) { e.printStackTrace(); } } }
package pipeInputOutput1; import java.io.PipedWriter; //输出流线程 public class WriteThread extends Thread { private WriteDate write; private PipedWriter out; public WriteThread(WriteDate write,PipedWriter out) { super(); this.write=write; this.out=out; } public void run() { write.writeMethod(out); } }
package pipeInputOutput1; import java.io.PipedReader; //输入流线程 public class ReadThread extends Thread{ private ReadDate read; private PipedReader in; public ReadThread(ReadDate read,PipedReader in) { super(); this.read=read; this.in=in; } public void run() { read.readMethod(in); } }
package pipeInputOutput1; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; //测试方法 public class run { public static void main(String[] args) { try { WriteDate write=new WriteDate(); ReadDate read=new ReadDate(); PipedWriter out=new PipedWriter(); PipedReader in=new PipedReader(); //连接输出流与输入流 out.connect(in); //in.connect(out); ReadThread threadread=new ReadThread(read,in); threadread.start(); Thread.sleep(2000); WriteThread threadwrite=new WriteThread(write,out); threadwrite.start(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
以上是Java多執行緒編之管線通訊的實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!