PipedInputStream,PipedOutputStream实例,线程间通信

转自:https://blog.csdn.net/u010002184/article/details/82898175
作者:二十六画生的博客
Java.io.PipedOutputStream and java.io.PipedInputStream has been introduced in JDK 1.0. PipedOutputStream and PipedInputStream both are connected to each other to create a communication pipe. PipedOutputStream is the sending end and PipedOutputStream is the receiving end of the pipe. Both ends should not be handled by single thread otherwise deadlock may occur. If any thread stops working, pipe is said to be broken.

在这里插入图片描述
最好不要在一个线程中使用,可能会发生死锁。

如果有任一个线程停止工作或发生异常,管道就会被破坏,不能正常的发送或接收。

实例1:线程池使用

 
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class PipedInputOutputDemo {
    final static PipedOutputStream pipedOut = new PipedOutputStream();//输出,发送端,写入流中
    final static PipedInputStream pipedIn = new PipedInputStream();//输入,接收端,从流中读取
 
    class PipedOutputThread implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 1; i <= 5; i++) {
                    pipedOut.write(("Message " + i + "\n").getBytes());
                    Thread.sleep(500);
                }
                pipedOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    class PipedInputThread implements Runnable {
        @Override
        public void run() {
            try {
                int i = 0;
                while ((i = pipedIn.read()) != -1) {
                    System.out.print((char) i);
                }
                pipedIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    public static void main(String[] args) {
        try {
            pipedOut.connect(pipedIn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.execute(new PipedInputOutputDemo().new PipedOutputThread());
        service.execute(new PipedInputOutputDemo().new PipedInputThread());
    }
}

输出:

Message 1
Message 2
Message 3
Message 4
Message 5

实例2:两个单独的线程使用

 
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
 
public class PipedInputOutputDemo {
    final static PipedOutputStream pipedOut = new PipedOutputStream();//输出,发送端,写入流中
    final static PipedInputStream pipedIn = new PipedInputStream();//输入,接收端,从流中读取
 
    class PipedOutputThread implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 1; i <= 5; i++) {
                    pipedOut.write(("Message " + i + "\n").getBytes());
                    Thread.sleep(500);
                }
                pipedOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    class PipedInputThread implements Runnable {
        @Override
        public void run() {
            try {
                int i = 0;
                while ((i = pipedIn.read()) != -1) {
                    System.out.print((char) i);
                }
                pipedIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    public void f1() {
        Thread pipedOutputThread = new Thread(new PipedOutputThread());
        Thread pipedInputThread = new Thread(new PipedInputThread());
        pipedOutputThread.start();
        pipedInputThread.start();
    }
 
 
    public static void main(String[] args) {
        try {
            pipedOut.connect(pipedIn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        new PipedInputOutputDemo().f1();
    }
}

输出:

Message 1
Message 2
Message 3
Message 4
Message 5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值