当前位置: 代码迷 >> 综合 >> 线程通信之管道方法(pipe)
  详细解决方案

线程通信之管道方法(pipe)

热度:8   发布时间:2023-12-07 05:17:52.0

1.如果想知道进程通信可访问my.oschina.net/u/248570/blog/53226可进行查看;

2.我这里主要讲解的是线程通信里的管道通信,共享内存通信里线程的消费者和生产者模式就是个很好的解释。

  (管道通信)

  这里要用到管道流

   PipedInputStram()用于读入

   PipedOutputStram()用于写入

   他们之间运用connect()方法进行管道间连接,进行通信;他们在线程与InputStram和OutoutStram一起连用,起到数据缓存的作用,就是把读取的数据存储到管道流里面,然后再用输入,输出流进行读取作用;

    编码思想:

       输入流InputStream和PipedOutputStram连用,先用InputStream把对应的地址数据读取出来,然后用PipedOutputStram把读取出来的数据写入到管道里面;

       输出流OutputStream和PipedInputStram连用,先用PipedInputStram把管道里的数据读取出来,然后用OutputStream把管道读取出来的数据写入到相对应的文件中;

       在写一个main方法进行测试,注意管道流是否连接

   编码内容:


      package com.io.test;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


public class pipleTest {


 public static void main(String[] args) throws Exception {
PipedInputStream pin=new PipedInputStream();
PipedOutputStream pou=new PipedOutputStream();
pou.connect(pin);
RW rw=new RW(pin, pou);
Thread a=new Thread(new R(rw),"读出:");
Thread b=new Thread(new W(rw),"写入:");
a.start();
b.start();
 
}
}


class RW{
//为管道流添加关闭条件判断
Boolean read_flag=true;
Boolean write_flag=true;
//判断条件(一时间作为判断条件)
String tt="01:09";
//管道流
PipedInputStream pin;
PipedOutputStream pou;
//构造
public  RW(PipedInputStream pin,PipedOutputStream pou) {
this.pin=pin;
this.pou=pou;
}
//读入方法
public synchronized void Read(){
try {
//目标目录
InputStream input=new FileInputStream("F:\\blog\\ThreadTest\\src\\a.txt");
System.out.println("R:读取数据中........");
byte  buff[]=new byte[1024];
int len;
try {
//判断InputStream流里是否有数据
if((len=input.read(buff))!=-1){
pou.write(buff, 0, len);
}
//关闭流
input.close();
//为管道流关闭添加事件 这里我用的是用时间进行限制
pipeClose(tt);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}


//写入方法
public synchronized void Write(){
try {
//写入的目标目录
OutputStream out=new FileOutputStream("F:\\blog\\ThreadTest\\src\\b.txt");
System.out.println("W:开始将数据写入:但等个5秒让我们观察..."+"\n");  
   try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}  //释放cpu执行权5秒  
byte  buff[]=new byte[1024];
int len;
try {
//判断管道流里的数据
if((len=pin.read(buff))!=-1){
System.out.println("W:开始将数据写入..."+"\n");  
out.write(buff, 0, len);
out.close();
//为管道流关闭添加事件 这里我用的是用时间进行限制
pipeClose(tt);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//关闭管道流的方法
public void pipeClose(String tt){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time=sdf.format(new Date());
if(time.substring(10,time.length()-3).trim().equals(tt)){
//判断管道流里的数据是否存在
if(pou!=null){
try {
read_flag=false;
pou.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(pin!=null){
try {
write_flag=false;
pin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}


//写入的线程
class R implements Runnable{
RW rw;
public R(RW rw) {
this.rw=rw;
}
@Override
public void run() {
//查看写入的管道流是否关闭
while(rw.read_flag){
rw.Read();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}


}


//读入的线程
class W implements Runnable{
RW rw;
public W(RW rw) {
this.rw=rw;
}
@Override
public void run() {
//查看读入的管道流是否关闭
while(rw.write_flag){
rw.Write();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}


}

效果图:





       

  相关解决方案