激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java多線程編程之管道通信詳解

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

2021-01-21 13:29WAUANG Java教程

這篇文章主要為大家詳細介紹了java多線程編程之線程間的通信,探討使用管道進行通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
  }
 }
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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();
  }
 }
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
 }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
 }
 
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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...

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
 
  }
 }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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();
  }
 }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
 }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
 }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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ù)組而已。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/qq_39266910/article/details/78201810

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品国产亚洲7777小说 | 99在线啪 | 国产精品一区二区视频 | 国产精品久久久久久久久久久久午夜 | 免费午夜视频在线观看 | 欧美a在线| 欧美视频黄色 | 久久久久久久免费精品 | 中国黄色一级生活片 | 精品中文字幕在线播放 | 精品一区二区免费视频视频 | 国产精品剧情一区二区在线观看 | 蜜桃视频在线观看视频 | 日韩黄色片在线观看 | 国产乱淫av一区二区三区 | 毛片在线播放视频 | av成人免费看 | 激情网站在线观看 | 欧美日韩一区二区综合 | 亚洲情在线 | 免费啪视频在线观看 | 91 久久 | 欧美日韩免费看 | 在线 日本 制服 中文 欧美 | 国产精品成人一区 | 欧美日韩在线免费观看 | 越南一级黄色片 | 欧美日韩免费在线观看视频 | 91久久国产露脸精品国产 | 91快色视频 | 成人在线视频精品 | 精品亚洲网站 | 国产成人高清成人av片在线看 | 黄色av一区二区三区 | 视频在线中文字幕 | 国产九色在线观看 | 在线成人一区二区 | 国产精品麻豆一区二区三区 | 一区二区三区欧美在线观看 | 国产99久久久久久免费看农村 | av在线免费观看网 |