首頁  >  文章  >  Java  >  Java多執行緒編之管線通訊的實例分析

Java多執行緒編之管線通訊的實例分析

黄舟
黄舟原創
2017-10-19 09:54:491865瀏覽

這篇文章主要為大家詳細介紹了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();
  }
 }

}

控制台輸出:

##read:

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn