溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java多線程編程之管道通信詳解

發(fā)布時間:2020-10-11 08:01:47 來源:腳本之家 閱讀:168 作者:WAUANG 欄目:編程語言

上一章節(jié)講了wait/notify通信,這一節(jié)我們來探討使用管道進(jìn)行通信。

java中提供了IO流使我們很方便的對數(shù)據(jù)進(jìn)行操作,pipeStream是一種特殊的流,用于不同線程間直接傳送數(shù)據(jù)。一個線程將數(shù)據(jù)發(fā)送到輸出管道,另一個線程從輸入管道讀取數(shù)據(jù)。通過管道實(shí)現(xiàn)通信不需要借助臨時文件這類東西。

java中提供了四個類使得線程間可以通信:

①字節(jié)流:PipeInputStream,PipedOutputStream
②字符流:PipedReader,PipedWriter

下面我們看看字節(jié)流的實(shí)現(xiàn)方法:

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();
   //輸出流與輸入流進(jìn)行連接。
   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...

上面測試中,先啟動輸入線程,然后因?yàn)闆]有線程被寫入所以線程被阻塞,知道有數(shù)據(jù)寫入。

我們接著繼續(xù)看看字符流的實(shí)現(xiàn)方法:

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();
  }
 }
}

字符流額字節(jié)流大同小異,上面的例子中字符流不需要創(chuàng)建字節(jié)數(shù)組而已。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI